From d1ab89deb79f70c439b58750d044d9cadf1194e5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 11 Jun 2019 11:54:33 +0200 Subject: Handle email update on server --- server/controllers/api/users/index.ts | 18 ++++++++---------- server/controllers/api/users/me.ts | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 11 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts index 99f51a648..c1d72087c 100644 --- a/server/controllers/api/users/index.ts +++ b/server/controllers/api/users/index.ts @@ -6,7 +6,7 @@ import { getFormattedObjects } from '../../../helpers/utils' import { RATES_LIMIT, WEBSERVER } from '../../../initializers/constants' import { Emailer } from '../../../lib/emailer' import { Redis } from '../../../lib/redis' -import { createUserAccountAndChannelAndPlaylist } from '../../../lib/user' +import { createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user' import { asyncMiddleware, asyncRetryTransactionMiddleware, @@ -147,7 +147,7 @@ usersRouter.post('/:id/reset-password', usersRouter.post('/ask-send-verify-email', askSendEmailLimiter, asyncMiddleware(usersAskSendVerifyEmailValidator), - asyncMiddleware(askSendVerifyUserEmail) + asyncMiddleware(reSendVerifyUserEmail) ) usersRouter.post('/:id/verify-email', @@ -320,14 +320,7 @@ async function resetUserPassword (req: express.Request, res: express.Response) { return res.status(204).end() } -async function sendVerifyUserEmail (user: UserModel) { - const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id) - const url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString - await Emailer.Instance.addVerifyEmailJob(user.email, url) - return -} - -async function askSendVerifyUserEmail (req: express.Request, res: express.Response) { +async function reSendVerifyUserEmail (req: express.Request, res: express.Response) { const user = res.locals.user await sendVerifyUserEmail(user) @@ -339,6 +332,11 @@ async function verifyUserEmail (req: express.Request, res: express.Response) { const user = res.locals.user user.emailVerified = true + if (req.body.isPendingEmail === true) { + user.email = user.pendingEmail + user.pendingEmail = null + } + await user.save() return res.status(204).end() diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index ddb239e7b..1750a02e9 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts @@ -28,6 +28,7 @@ import { VideoImportModel } from '../../../models/video/video-import' import { AccountModel } from '../../../models/account/account' import { CONFIG } from '../../../initializers/config' import { sequelizeTypescript } from '../../../initializers/database' +import { sendVerifyUserEmail } from '../../../lib/user' const auditLogger = auditLoggerFactory('users-me') @@ -171,17 +172,26 @@ async function deleteMe (req: express.Request, res: express.Response) { async function updateMe (req: express.Request, res: express.Response) { const body: UserUpdateMe = req.body + let sendVerificationEmail = false const user = res.locals.oauth.token.user const oldUserAuditView = new UserAuditView(user.toFormattedJSON({})) if (body.password !== undefined) user.password = body.password - if (body.email !== undefined) user.email = body.email if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo if (body.videosHistoryEnabled !== undefined) user.videosHistoryEnabled = body.videosHistoryEnabled + if (body.email !== undefined) { + if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) { + user.pendingEmail = body.email + sendVerificationEmail = true + } else { + user.email = body.email + } + } + await sequelizeTypescript.transaction(async t => { const userAccount = await AccountModel.load(user.Account.id) @@ -196,6 +206,10 @@ async function updateMe (req: express.Request, res: express.Response) { auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON({})), oldUserAuditView) }) + if (sendVerificationEmail === true) { + await sendVerifyUserEmail(user, true) + } + return res.sendStatus(204) } -- cgit v1.2.3