@@ -27,12 +27,13 @@ import {
2727 updateChat ,
2828 updateConfig ,
2929 updateUserInfo ,
30+ updateUserPassword ,
3031 verifyUser ,
3132} from './storage/mongo'
3233import { limiter } from './middleware/limiter'
3334import { isEmail , isNotEmptyString } from './utils/is'
34- import { sendNoticeMail , sendTestMail , sendVerifyMail , sendVerifyMailAdmin } from './utils/mail'
35- import { checkUserVerify , checkUserVerifyAdmin , getUserVerifyUrl , getUserVerifyUrlAdmin , md5 } from './utils/security'
35+ import { sendNoticeMail , sendResetPasswordMail , sendTestMail , sendVerifyMail , sendVerifyMailAdmin } from './utils/mail'
36+ import { checkUserResetPassword , checkUserVerify , checkUserVerifyAdmin , getUserResetPasswordUrl , getUserVerifyUrl , getUserVerifyUrlAdmin , md5 } from './utils/security'
3637import { rootAuth } from './middleware/rootAuth'
3738
3839dotenv . config ( )
@@ -468,6 +469,43 @@ router.post('/user-login', async (req, res) => {
468469 }
469470} )
470471
472+ router . post ( '/user-send-reset-mail' , async ( req , res ) => {
473+ try {
474+ const { username } = req . body as { username : string }
475+ if ( ! username || ! isEmail ( username ) )
476+ throw new Error ( '请输入格式正确的邮箱 | Please enter a correctly formatted email address.' )
477+
478+ const user = await getUser ( username )
479+ if ( user == null || user . status !== Status . Normal )
480+ throw new Error ( '账户状态异常 | Account status abnormal.' )
481+ await sendResetPasswordMail ( username , await getUserResetPasswordUrl ( username ) )
482+ res . send ( { status : 'Success' , message : '重置邮件已发送 | Reset email has been sent' , data : null } )
483+ }
484+ catch ( error ) {
485+ res . send ( { status : 'Fail' , message : error . message , data : null } )
486+ }
487+ } )
488+
489+ router . post ( '/user-reset-password' , async ( req , res ) => {
490+ try {
491+ const { username, password, sign } = req . body as { username : string ; password : string ; sign : string }
492+ if ( ! username || ! password || ! isEmail ( username ) )
493+ throw new Error ( '用户名或密码为空 | Username or password is empty' )
494+ if ( ! sign || ! checkUserResetPassword ( sign , username ) )
495+ throw new Error ( '链接失效, 请重新发送 | The link is invalid, please resend.' )
496+ const user = await getUser ( username )
497+ if ( user == null || user . status !== Status . Normal )
498+ throw new Error ( '账户状态异常 | Account status abnormal.' )
499+
500+ updateUserPassword ( user . _id . toString ( ) , md5 ( password ) )
501+
502+ res . send ( { status : 'Success' , message : '密码重置成功 | Password reset successful' , data : null } )
503+ }
504+ catch ( error ) {
505+ res . send ( { status : 'Fail' , message : error . message , data : null } )
506+ }
507+ } )
508+
471509router . post ( '/user-info' , auth , async ( req , res ) => {
472510 try {
473511 const { name, avatar, description } = req . body as UserInfo
0 commit comments