@@ -2,12 +2,13 @@ import async from "async";
2
2
import crypto from "crypto" ;
3
3
import nodemailer from "nodemailer" ;
4
4
import passport from "passport" ;
5
- import { User , UserDocument , AuthToken } from "../models/User" ;
6
- import { Request , Response , NextFunction } from "express" ;
7
- import { IVerifyOptions } from "passport-local" ;
8
- import { WriteError } from "mongodb" ;
9
- import { check , sanitize , validationResult } from "express-validator" ;
5
+ import { AuthToken , User , UserDocument } from "../models/User" ;
6
+ import { Request , Response } from "express" ;
7
+ import { IVerifyOptions } from "passport-local" ;
8
+ import { WriteError } from "mongodb" ;
9
+ import { check , sanitize , validationResult } from "express-validator" ;
10
10
import "../config/passport" ;
11
+ import { genericExpressMethod } from "express-request-with-user" ;
11
12
12
13
/**
13
14
* GET /login
@@ -26,7 +27,8 @@ export const getLogin = (req: Request, res: Response) => {
26
27
* POST /login
27
28
* Sign in using email and password.
28
29
*/
29
- export const postLogin = ( req : Request , res : Response , next : NextFunction ) => {
30
+
31
+ export const postLogin : genericExpressMethod = ( req , res , next ) => {
30
32
check ( "email" , "Email is not valid" ) . isEmail ( ) ;
31
33
check ( "password" , "Password cannot be blank" ) . isLength ( { min : 1 } ) ;
32
34
// eslint-disable-next-line @typescript-eslint/camelcase
@@ -79,7 +81,7 @@ export const getSignup = (req: Request, res: Response) => {
79
81
* POST /signup
80
82
* Create a new local account.
81
83
*/
82
- export const postSignup = ( req : Request , res : Response , next : NextFunction ) => {
84
+ export const postSignup : genericExpressMethod = ( req , res , next ) => {
83
85
check ( "email" , "Email is not valid" ) . isEmail ( ) ;
84
86
check ( "password" , "Password must be at least 4 characters long" ) . isLength ( { min : 4 } ) ;
85
87
check ( "confirmPassword" , "Passwords do not match" ) . equals ( req . body . password ) ;
@@ -130,7 +132,7 @@ export const getAccount = (req: Request, res: Response) => {
130
132
* POST /account/profile
131
133
* Update profile information.
132
134
*/
133
- export const postUpdateProfile = ( req : Request , res : Response , next : NextFunction ) => {
135
+ export const postUpdateProfile : genericExpressMethod = ( req , res , next ) => {
134
136
check ( "email" , "Please enter a valid email address." ) . isEmail ( ) ;
135
137
// eslint-disable-next-line @typescript-eslint/camelcase
136
138
sanitize ( "email" ) . normalizeEmail ( { gmail_remove_dots : false } ) ;
@@ -142,8 +144,8 @@ export const postUpdateProfile = (req: Request, res: Response, next: NextFunctio
142
144
return res . redirect ( "/account" ) ;
143
145
}
144
146
145
- const user = req . user as UserDocument ;
146
- User . findById ( user . id , ( err , user : UserDocument ) => {
147
+ // const user = req.user as UserDocument;
148
+ User . findById ( req . user . id , ( err , user ) => {
147
149
if ( err ) { return next ( err ) ; }
148
150
user . email = req . body . email || "" ;
149
151
user . profile . name = req . body . name || "" ;
@@ -168,7 +170,7 @@ export const postUpdateProfile = (req: Request, res: Response, next: NextFunctio
168
170
* POST /account/password
169
171
* Update current password.
170
172
*/
171
- export const postUpdatePassword = ( req : Request , res : Response , next : NextFunction ) => {
173
+ export const postUpdatePassword : genericExpressMethod = ( req , res , next ) => {
172
174
check ( "password" , "Password must be at least 4 characters long" ) . isLength ( { min : 4 } ) ;
173
175
check ( "confirmPassword" , "Passwords do not match" ) . equals ( req . body . password ) ;
174
176
@@ -179,8 +181,8 @@ export const postUpdatePassword = (req: Request, res: Response, next: NextFuncti
179
181
return res . redirect ( "/account" ) ;
180
182
}
181
183
182
- const user = req . user as UserDocument ;
183
- User . findById ( user . id , ( err , user : UserDocument ) => {
184
+ // const user = req.user as UserDocument;
185
+ User . findById ( req . user . id , ( err , user ) => {
184
186
if ( err ) { return next ( err ) ; }
185
187
user . password = req . body . password ;
186
188
user . save ( ( err : WriteError ) => {
@@ -195,9 +197,9 @@ export const postUpdatePassword = (req: Request, res: Response, next: NextFuncti
195
197
* POST /account/delete
196
198
* Delete user account.
197
199
*/
198
- export const postDeleteAccount = ( req : Request , res : Response , next : NextFunction ) => {
199
- const user = req . user as UserDocument ;
200
- User . remove ( { _id : user . id } , ( err ) => {
200
+ export const postDeleteAccount : genericExpressMethod = ( req , res , next ) => {
201
+ // const user = req.user as UserDocument;
202
+ User . remove ( { _id : req . user . id } , ( err ) => {
201
203
if ( err ) { return next ( err ) ; }
202
204
req . logout ( ) ;
203
205
req . flash ( "info" , { msg : "Your account has been deleted." } ) ;
@@ -209,12 +211,12 @@ export const postDeleteAccount = (req: Request, res: Response, next: NextFunctio
209
211
* GET /account/unlink/:provider
210
212
* Unlink OAuth provider.
211
213
*/
212
- export const getOauthUnlink = ( req : Request , res : Response , next : NextFunction ) => {
214
+ export const getOauthUnlink : genericExpressMethod = ( req , res , next ) => {
213
215
const provider = req . params . provider ;
214
- const user = req . user as UserDocument ;
215
- User . findById ( user . id , ( err , user : any ) => {
216
+ // const user = req.user as UserDocument;
217
+ User . findById ( req . user . id , ( err , user ) => {
216
218
if ( err ) { return next ( err ) ; }
217
- user [ provider ] = undefined ;
219
+ // user[provider] = undefined;
218
220
user . tokens = user . tokens . filter ( ( token : AuthToken ) => token . kind !== provider ) ;
219
221
user . save ( ( err : WriteError ) => {
220
222
if ( err ) { return next ( err ) ; }
@@ -228,7 +230,7 @@ export const getOauthUnlink = (req: Request, res: Response, next: NextFunction)
228
230
* GET /reset/:token
229
231
* Reset Password page.
230
232
*/
231
- export const getReset = ( req : Request , res : Response , next : NextFunction ) => {
233
+ export const getReset : genericExpressMethod = ( req , res , next ) => {
232
234
if ( req . isAuthenticated ( ) ) {
233
235
return res . redirect ( "/" ) ;
234
236
}
@@ -251,7 +253,7 @@ export const getReset = (req: Request, res: Response, next: NextFunction) => {
251
253
* POST /reset/:token
252
254
* Process the reset password request.
253
255
*/
254
- export const postReset = ( req : Request , res : Response , next : NextFunction ) => {
256
+ export const postReset : genericExpressMethod = ( req , res , next ) => {
255
257
check ( "password" , "Password must be at least 4 characters long." ) . isLength ( { min : 4 } ) ;
256
258
check ( "confirm" , "Passwords must match." ) . equals ( req . body . password ) ;
257
259
@@ -267,7 +269,7 @@ export const postReset = (req: Request, res: Response, next: NextFunction) => {
267
269
User
268
270
. findOne ( { passwordResetToken : req . params . token } )
269
271
. where ( "passwordResetExpires" ) . gt ( Date . now ( ) )
270
- . exec ( ( err , user : any ) => {
272
+ . exec ( ( err , user ) => {
271
273
if ( err ) { return next ( err ) ; }
272
274
if ( ! user ) {
273
275
req . flash ( "errors" , { msg : "Password reset token is invalid or has expired." } ) ;
@@ -326,7 +328,7 @@ export const getForgot = (req: Request, res: Response) => {
326
328
* POST /forgot
327
329
* Create a random token, then the send user an email with a reset link.
328
330
*/
329
- export const postForgot = ( req : Request , res : Response , next : NextFunction ) => {
331
+ export const postForgot : genericExpressMethod = ( req , res , next ) => {
330
332
check ( "email" , "Please enter a valid email address." ) . isEmail ( ) ;
331
333
// eslint-disable-next-line @typescript-eslint/camelcase
332
334
sanitize ( "email" ) . normalizeEmail ( { gmail_remove_dots : false } ) ;
@@ -346,14 +348,14 @@ export const postForgot = (req: Request, res: Response, next: NextFunction) => {
346
348
} ) ;
347
349
} ,
348
350
function setRandomToken ( token : AuthToken , done : Function ) {
349
- User . findOne ( { email : req . body . email } , ( err , user : any ) => {
351
+ User . findOne ( { email : req . body . email } , ( err , user ) => {
350
352
if ( err ) { return done ( err ) ; }
351
353
if ( ! user ) {
352
354
req . flash ( "errors" , { msg : "Account with that email address does not exist." } ) ;
353
355
return res . redirect ( "/forgot" ) ;
354
356
}
355
357
user . passwordResetToken = token ;
356
- user . passwordResetExpires = Date . now ( ) + 3600000 ; // 1 hour
358
+ user . passwordResetExpires = new Date ( Date . now ( ) + 3600000 ) ; // 1 hour
357
359
user . save ( ( err : WriteError ) => {
358
360
done ( err , token , user ) ;
359
361
} ) ;
0 commit comments