diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2018-11-29 16:23:45 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2018-11-29 16:23:45 +0900 |
| commit | 1bc109b42c43dfa0dcad4b1331896ab5b2023892 (patch) | |
| tree | 2185b3408754f78ad03b991e3c78415c6dee111b /src/server/api/endpoints/i | |
| parent | 10.58.2 (diff) | |
| download | misskey-1bc109b42c43dfa0dcad4b1331896ab5b2023892.tar.gz misskey-1bc109b42c43dfa0dcad4b1331896ab5b2023892.tar.bz2 misskey-1bc109b42c43dfa0dcad4b1331896ab5b2023892.zip | |
Implement email config
Diffstat (limited to 'src/server/api/endpoints/i')
| -rw-r--r-- | src/server/api/endpoints/i/update_email.ts | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/server/api/endpoints/i/update_email.ts b/src/server/api/endpoints/i/update_email.ts new file mode 100644 index 0000000000..c2699d47c2 --- /dev/null +++ b/src/server/api/endpoints/i/update_email.ts @@ -0,0 +1,85 @@ +import $ from 'cafy'; +import User, { pack } from '../../../../models/user'; +import { publishMainStream } from '../../../../stream'; +import define from '../../define'; +import * as nodemailer from 'nodemailer'; +import fetchMeta from '../../../../misc/fetch-meta'; +import rndstr from 'rndstr'; +import config from '../../../../config'; +const ms = require('ms'); + +export const meta = { + requireCredential: true, + + secure: true, + + limit: { + duration: ms('1hour'), + max: 3 + }, + + params: { + email: { + validator: $.str.optional.nullable + }, + } +}; + +export default define(meta, (ps, user) => new Promise(async (res, rej) => { + await User.update(user._id, { + $set: { + email: ps.email, + emailVerified: false, + emailVerifyCode: null + } + }); + + // Serialize + const iObj = await pack(user._id, user, { + detail: true, + includeSecrets: true + }); + + // Send response + res(iObj); + + // Publish meUpdated event + publishMainStream(user._id, 'meUpdated', iObj); + + if (ps.email != null) { + const code = rndstr('a-z0-9', 16); + + await User.update(user._id, { + $set: { + emailVerifyCode: code + } + }); + + const meta = await fetchMeta(); + + const transporter = nodemailer.createTransport({ + host: meta.smtpHost, + port: meta.smtpPort, + secure: meta.smtpSecure, + auth: { + user: meta.smtpUser, + pass: meta.smtpPass + } + }); + + const link = `${config.url}/vefify-email/${code}`; + + transporter.sendMail({ + from: meta.email, + to: ps.email, + subject: meta.name, + text: `To verify email, please click this link: ${link}` + }, (error, info) => { + if (error) { + return console.error(error); + } + + console.log('Message sent: %s', info.messageId); + }); + } +})); |