mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-04 02:09:37 +02:00
Better ask email verification flow
Allow user to resend the email verification link when changing the current email Fix success messages when validating a new email
This commit is contained in:
parent
e19ee1ebc9
commit
986e71a1f7
29 changed files with 426 additions and 271 deletions
|
@ -1,11 +1,12 @@
|
|||
import express from 'express'
|
||||
import { HttpStatusCode } from '@peertube/peertube-models'
|
||||
import express from 'express'
|
||||
import { CONFIG } from '../../../initializers/config.js'
|
||||
import { sendVerifyRegistrationEmail, sendVerifyUserEmail } from '../../../lib/user.js'
|
||||
import { sendVerifyRegistrationEmail, sendVerifyRegistrationRequestEmail, sendVerifyUserChangeEmail } from '../../../lib/user.js'
|
||||
import { asyncMiddleware, buildRateLimiter } from '../../../middlewares/index.js'
|
||||
import {
|
||||
registrationVerifyEmailValidator,
|
||||
usersAskSendVerifyEmailValidator,
|
||||
usersAskSendRegistrationVerifyEmailValidator,
|
||||
usersAskSendUserVerifyEmailValidator,
|
||||
usersVerifyEmailValidator
|
||||
} from '../../../middlewares/validators/index.js'
|
||||
|
||||
|
@ -16,18 +17,24 @@ const askSendEmailLimiter = buildRateLimiter({
|
|||
|
||||
const emailVerificationRouter = express.Router()
|
||||
|
||||
emailVerificationRouter.post([ '/ask-send-verify-email', '/registrations/ask-send-verify-email' ],
|
||||
emailVerificationRouter.post(
|
||||
'/ask-send-verify-email',
|
||||
askSendEmailLimiter,
|
||||
asyncMiddleware(usersAskSendVerifyEmailValidator),
|
||||
asyncMiddleware(reSendVerifyUserEmail)
|
||||
asyncMiddleware(usersAskSendUserVerifyEmailValidator),
|
||||
asyncMiddleware(reSendUserVerifyUserEmail)
|
||||
)
|
||||
|
||||
emailVerificationRouter.post('/:id/verify-email',
|
||||
asyncMiddleware(usersVerifyEmailValidator),
|
||||
asyncMiddleware(verifyUserEmail)
|
||||
emailVerificationRouter.post(
|
||||
'/registrations/ask-send-verify-email',
|
||||
askSendEmailLimiter,
|
||||
asyncMiddleware(usersAskSendRegistrationVerifyEmailValidator),
|
||||
asyncMiddleware(reSendRegistrationVerifyUserEmail)
|
||||
)
|
||||
|
||||
emailVerificationRouter.post('/registrations/:registrationId/verify-email',
|
||||
emailVerificationRouter.post('/:id/verify-email', asyncMiddleware(usersVerifyEmailValidator), asyncMiddleware(verifyUserEmail))
|
||||
|
||||
emailVerificationRouter.post(
|
||||
'/registrations/:registrationId/verify-email',
|
||||
asyncMiddleware(registrationVerifyEmailValidator),
|
||||
asyncMiddleware(verifyRegistrationEmail)
|
||||
)
|
||||
|
@ -38,14 +45,20 @@ export {
|
|||
emailVerificationRouter
|
||||
}
|
||||
|
||||
async function reSendVerifyUserEmail (req: express.Request, res: express.Response) {
|
||||
const user = res.locals.user
|
||||
const registration = res.locals.userRegistration
|
||||
async function reSendUserVerifyUserEmail (req: express.Request, res: express.Response) {
|
||||
if (res.locals.userPendingEmail) { // User wants to change its current email
|
||||
await sendVerifyUserChangeEmail(res.locals.userPendingEmail)
|
||||
} else { // After an account creation
|
||||
await sendVerifyRegistrationEmail(res.locals.userEmail)
|
||||
}
|
||||
|
||||
if (user) await sendVerifyUserEmail(user)
|
||||
else if (registration) await sendVerifyRegistrationEmail(registration)
|
||||
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
||||
}
|
||||
|
||||
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
||||
async function reSendRegistrationVerifyUserEmail (req: express.Request, res: express.Response) {
|
||||
await sendVerifyRegistrationRequestEmail(res.locals.userRegistration)
|
||||
|
||||
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
||||
}
|
||||
|
||||
async function verifyUserEmail (req: express.Request, res: express.Response) {
|
||||
|
@ -59,7 +72,7 @@ async function verifyUserEmail (req: express.Request, res: express.Response) {
|
|||
|
||||
await user.save()
|
||||
|
||||
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
||||
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
||||
}
|
||||
|
||||
async function verifyRegistrationEmail (req: express.Request, res: express.Response) {
|
||||
|
@ -68,5 +81,5 @@ async function verifyRegistrationEmail (req: express.Request, res: express.Respo
|
|||
|
||||
await registration.save()
|
||||
|
||||
return res.status(HttpStatusCode.NO_CONTENT_204).end()
|
||||
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue