@@ -6,7 +6,7 @@ import { User, UserDocument, AuthToken } from "../models/User";
6
6
import { Request , Response , NextFunction } from "express" ;
7
7
import { IVerifyOptions } from "passport-local" ;
8
8
import { WriteError } from "mongodb" ;
9
- import request from "express-validator" ;
9
+ import { check , sanitize , validationResult } from "express-validator" ;
10
10
import "../config/passport" ;
11
11
12
12
/**
@@ -27,21 +27,21 @@ export const getLogin = (req: Request, res: Response) => {
27
27
* Sign in using email and password.
28
28
*/
29
29
export const postLogin = ( req : Request , res : Response , next : NextFunction ) => {
30
- req . assert ( "email" , "Email is not valid" ) . isEmail ( ) ;
31
- req . assert ( "password" , "Password cannot be blank" ) . notEmpty ( ) ;
32
- req . sanitize ( "email" ) . normalizeEmail ( { gmail_remove_dots : false } ) ;
30
+ check ( "email" , "Email is not valid" ) . isEmail ( ) ;
31
+ check ( "password" , "Password cannot be blank" ) . isLength ( { min : 1 } ) ;
32
+ sanitize ( "email" ) . normalizeEmail ( { gmail_remove_dots : false } ) ;
33
33
34
- const errors = req . validationErrors ( ) ;
34
+ const errors = validationResult ( req ) ;
35
35
36
- if ( errors ) {
37
- req . flash ( "errors" , errors ) ;
36
+ if ( ! errors . isEmpty ( ) ) {
37
+ req . flash ( "errors" , errors . array ( ) ) ;
38
38
return res . redirect ( "/login" ) ;
39
39
}
40
40
41
41
passport . authenticate ( "local" , ( err : Error , user : UserDocument , info : IVerifyOptions ) => {
42
42
if ( err ) { return next ( err ) ; }
43
43
if ( ! user ) {
44
- req . flash ( "errors" , info . message ) ;
44
+ req . flash ( "errors" , { msg : info . message } ) ;
45
45
return res . redirect ( "/login" ) ;
46
46
}
47
47
req . logIn ( user , ( err ) => {
@@ -79,15 +79,15 @@ export const getSignup = (req: Request, res: Response) => {
79
79
* Create a new local account.
80
80
*/
81
81
export const postSignup = ( req : Request , res : Response , next : NextFunction ) => {
82
- req . assert ( "email" , "Email is not valid" ) . isEmail ( ) ;
83
- req . assert ( "password" , "Password must be at least 4 characters long" ) . len ( { min : 4 } ) ;
84
- req . assert ( "confirmPassword" , "Passwords do not match" ) . equals ( req . body . password ) ;
85
- req . sanitize ( "email" ) . normalizeEmail ( { gmail_remove_dots : false } ) ;
82
+ check ( "email" , "Email is not valid" ) . isEmail ( ) ;
83
+ check ( "password" , "Password must be at least 4 characters long" ) . isLength ( { min : 4 } ) ;
84
+ check ( "confirmPassword" , "Passwords do not match" ) . equals ( req . body . password ) ;
85
+ sanitize ( "email" ) . normalizeEmail ( { gmail_remove_dots : false } ) ;
86
86
87
- const errors = req . validationErrors ( ) ;
87
+ const errors = validationResult ( req ) ;
88
88
89
- if ( errors ) {
90
- req . flash ( "errors" , errors ) ;
89
+ if ( ! errors . isEmpty ( ) ) {
90
+ req . flash ( "errors" , errors . array ( ) ) ;
91
91
return res . redirect ( "/signup" ) ;
92
92
}
93
93
@@ -129,13 +129,13 @@ export const getAccount = (req: Request, res: Response) => {
129
129
* Update profile information.
130
130
*/
131
131
export const postUpdateProfile = ( req : Request , res : Response , next : NextFunction ) => {
132
- req . assert ( "email" , "Please enter a valid email address." ) . isEmail ( ) ;
133
- req . sanitize ( "email" ) . normalizeEmail ( { gmail_remove_dots : false } ) ;
132
+ check ( "email" , "Please enter a valid email address." ) . isEmail ( ) ;
133
+ sanitize ( "email" ) . normalizeEmail ( { gmail_remove_dots : false } ) ;
134
134
135
- const errors = req . validationErrors ( ) ;
135
+ const errors = validationResult ( req ) ;
136
136
137
- if ( errors ) {
138
- req . flash ( "errors" , errors ) ;
137
+ if ( ! errors . isEmpty ( ) ) {
138
+ req . flash ( "errors" , errors . array ( ) ) ;
139
139
return res . redirect ( "/account" ) ;
140
140
}
141
141
@@ -165,13 +165,13 @@ export const postUpdateProfile = (req: Request, res: Response, next: NextFunctio
165
165
* Update current password.
166
166
*/
167
167
export const postUpdatePassword = ( req : Request , res : Response , next : NextFunction ) => {
168
- req . assert ( "password" , "Password must be at least 4 characters long" ) . len ( { min : 4 } ) ;
169
- req . assert ( "confirmPassword" , "Passwords do not match" ) . equals ( req . body . password ) ;
168
+ check ( "password" , "Password must be at least 4 characters long" ) . isLength ( { min : 4 } ) ;
169
+ check ( "confirmPassword" , "Passwords do not match" ) . equals ( req . body . password ) ;
170
170
171
- const errors = req . validationErrors ( ) ;
171
+ const errors = validationResult ( req ) ;
172
172
173
- if ( errors ) {
174
- req . flash ( "errors" , errors ) ;
173
+ if ( ! errors . isEmpty ( ) ) {
174
+ req . flash ( "errors" , errors . array ( ) ) ;
175
175
return res . redirect ( "/account" ) ;
176
176
}
177
177
@@ -245,13 +245,13 @@ export const getReset = (req: Request, res: Response, next: NextFunction) => {
245
245
* Process the reset password request.
246
246
*/
247
247
export const postReset = ( req : Request , res : Response , next : NextFunction ) => {
248
- req . assert ( "password" , "Password must be at least 4 characters long." ) . len ( { min : 4 } ) ;
249
- req . assert ( "confirm" , "Passwords must match." ) . equals ( req . body . password ) ;
248
+ check ( "password" , "Password must be at least 4 characters long." ) . isLength ( { min : 4 } ) ;
249
+ check ( "confirm" , "Passwords must match." ) . equals ( req . body . password ) ;
250
250
251
- const errors = req . validationErrors ( ) ;
251
+ const errors = validationResult ( req ) ;
252
252
253
- if ( errors ) {
254
- req . flash ( "errors" , errors ) ;
253
+ if ( ! errors . isEmpty ( ) ) {
254
+ req . flash ( "errors" , errors . array ( ) ) ;
255
255
return res . redirect ( "back" ) ;
256
256
}
257
257
@@ -320,13 +320,13 @@ export const getForgot = (req: Request, res: Response) => {
320
320
* Create a random token, then the send user an email with a reset link.
321
321
*/
322
322
export const postForgot = ( req : Request , res : Response , next : NextFunction ) => {
323
- req . assert ( "email" , "Please enter a valid email address." ) . isEmail ( ) ;
324
- req . sanitize ( "email" ) . normalizeEmail ( { gmail_remove_dots : false } ) ;
323
+ check ( "email" , "Please enter a valid email address." ) . isEmail ( ) ;
324
+ sanitize ( "email" ) . normalizeEmail ( { gmail_remove_dots : false } ) ;
325
325
326
- const errors = req . validationErrors ( ) ;
326
+ const errors = validationResult ( req ) ;
327
327
328
- if ( errors ) {
329
- req . flash ( "errors" , errors ) ;
328
+ if ( ! errors . isEmpty ( ) ) {
329
+ req . flash ( "errors" , errors . array ( ) ) ;
330
330
return res . redirect ( "/forgot" ) ;
331
331
}
332
332
0 commit comments