@@ -15,6 +15,8 @@ import {
15
15
} from "../model/repository.js" ;
16
16
import { BadRequestError , ConflictError , NotFoundError } from "../utils/httpErrors.js" ;
17
17
import TokenService from "../services/tokenService.js" ;
18
+ import { sendEmail } from "../services/emailService.js" ;
19
+ import redisService from "../services/redisService.js" ;
18
20
19
21
export async function createUser ( req , res , next ) {
20
22
try {
@@ -42,12 +44,11 @@ export async function createUser(req, res, next) {
42
44
data : { accessToken, user : { ...formatUserResponse ( createdUser ) } } ,
43
45
} ) ;
44
46
} catch ( err ) {
45
- console . error ( ' Error creating user:' , err ) ;
47
+ console . error ( " Error creating user:" , err ) ;
46
48
next ( err ) ;
47
49
}
48
50
}
49
51
50
-
51
52
export async function getUser ( req , res , next ) {
52
53
try {
53
54
const userId = req . params . id ;
@@ -162,6 +163,25 @@ export async function deleteUser(req, res, next) {
162
163
}
163
164
}
164
165
166
+ export async function forgetPassword ( req , res , next ) {
167
+ try {
168
+ const { email } = req . body ;
169
+ const emailToken = TokenService . generateEmailToken ( email ) ;
170
+ redisService . setKeyWithExpiration ( email , emailToken , 300 ) ;
171
+
172
+ const resetPasswordLink = `http://localhost:3000/reset-password/${ emailToken } ` ;
173
+ await sendEmail ( {
174
+ to : email ,
175
+ subject : "Reset password" ,
176
+ html : `Click <a href=${ resetPasswordLink } >here</a> to reset your password` ,
177
+ } ) ;
178
+
179
+ res . sendStatus ( 204 ) ;
180
+ } catch ( err ) {
181
+ next ( err ) ;
182
+ }
183
+ }
184
+
165
185
export function formatUserResponse ( user ) {
166
186
return {
167
187
_id : user . id ,
0 commit comments