1+ const { promisify } = require ( 'util' ) ;
2+ const crypto = require ( 'crypto' ) ;
3+ const passport = require ( 'passport' ) ;
4+ const validator = require ( 'validator' ) ;
5+ const User = require ( '../models/Users' ) ;
6+
7+ const randomBytesAsync = promisify ( crypto . randomBytes ) ;
8+
9+ /**
10+ * GET /login
11+ * Login page.
12+ */
13+ exports . getLogin = ( req , res ) => {
14+ if ( req . user ) {
15+ return res . redirect ( '/' ) ;
16+ }
17+ res . render ( 'account/login' , {
18+ title : 'Login'
19+ } ) ;
20+ } ;
21+
22+ /**
23+ * POST /login
24+ * Sign in using email and password.
25+ */
26+ exports . postLogin = ( req , res , next ) => {
27+ const validationErrors = [ ] ;
28+ if ( ! validator . isEmail ( req . body . email ) ) validationErrors . push ( { msg : 'Please enter a valid email address.' } ) ;
29+ if ( validator . isEmpty ( req . body . password ) ) validationErrors . push ( { msg : 'Password cannot be blank.' } ) ;
30+
31+ if ( validationErrors . length ) {
32+ req . flash ( 'errors' , validationErrors ) ;
33+ return res . redirect ( '/login' ) ;
34+ }
35+ req . body . email = validator . normalizeEmail ( req . body . email , { gmail_remove_dots : false } ) ;
36+
37+ passport . authenticate ( 'local' , ( err , user , info ) => {
38+ if ( err ) { return next ( err ) ; }
39+ if ( ! user ) {
40+ req . flash ( 'errors' , info ) ;
41+ return res . redirect ( '/login' ) ;
42+ }
43+ req . logIn ( user , ( err ) => {
44+ if ( err ) { return next ( err ) ; }
45+ req . flash ( 'success' , { msg : 'Success! You are logged in.' } ) ;
46+ res . redirect ( req . session . returnTo || '/' ) ;
47+ } ) ;
48+ } ) ( req , res , next ) ;
49+ } ;
50+
51+ /**
52+ * GET /logout
53+ * Log out.
54+ */
55+ exports . logout = ( req , res ) => {
56+ req . logout ( ) ;
57+ req . session . destroy ( ( err ) => {
58+ if ( err ) console . log ( 'Error : Failed to destroy the session during logout.' , err ) ;
59+ req . user = null ;
60+ res . redirect ( '/' ) ;
61+ } ) ;
62+ } ;
63+
64+ /**
65+ * GET /signup
66+ * Signup page.
67+ */
68+ exports . getSignup = ( req , res ) => {
69+ if ( req . user ) {
70+ return res . redirect ( '/' ) ;
71+ }
72+ res . render ( 'account/signup' , {
73+ title : 'Create Account'
74+ } ) ;
75+ } ;
76+
77+ /**
78+ * POST /signup
79+ * Create a new local account.
80+ */
81+ exports . postSignup = ( req , res , next ) => {
82+ const validationErrors = [ ] ;
83+ if ( ! validator . isEmail ( req . body . email ) ) validationErrors . push ( { msg : 'Please enter a valid email address.' } ) ;
84+ if ( ! validator . isLength ( req . body . password , { min : 8 } ) ) validationErrors . push ( { msg : 'Password must be at least 8 characters long' } ) ;
85+ if ( req . body . password !== req . body . confirmPassword ) validationErrors . push ( { msg : 'Passwords do not match' } ) ;
86+
87+ if ( validationErrors . length ) {
88+ req . flash ( 'errors' , validationErrors ) ;
89+ return res . redirect ( '/signup' ) ;
90+ }
91+ req . body . email = validator . normalizeEmail ( req . body . email , { gmail_remove_dots : false } ) ;
92+
93+ const user = new User ( {
94+ email : req . body . email ,
95+ password : req . body . password
96+ } ) ;
97+
98+ User . findOne ( { email : req . body . email } , ( err , existingUser ) => {
99+ if ( err ) { return next ( err ) ; }
100+ if ( existingUser ) {
101+ req . flash ( 'errors' , { msg : 'Account with that email address already exists.' } ) ;
102+ return res . redirect ( '/signup' ) ;
103+ }
104+ user . save ( ( err ) => {
105+ if ( err ) { return next ( err ) ; }
106+ req . logIn ( user , ( err ) => {
107+ if ( err ) {
108+ return next ( err ) ;
109+ }
110+ res . redirect ( '/' ) ;
111+ } ) ;
112+ } ) ;
113+ } ) ;
114+ } ;
115+
116+ /**
117+ * GET /account
118+ * Profile page.
119+ */
120+ exports . getAccount = ( req , res ) => {
121+ res . render ( 'account/profile' , {
122+ title : 'Account Management'
123+ } ) ;
124+ } ;
125+
126+ /**
127+ * POST /account/profile
128+ * Update profile information.
129+ */
130+ exports . postUpdateProfile = ( req , res , next ) => {
131+ const validationErrors = [ ] ;
132+ if ( ! validator . isEmail ( req . body . email ) ) validationErrors . push ( { msg : 'Please enter a valid email address.' } ) ;
133+
134+ if ( validationErrors . length ) {
135+ req . flash ( 'errors' , validationErrors ) ;
136+ return res . redirect ( '/account' ) ;
137+ }
138+ req . body . email = validator . normalizeEmail ( req . body . email , { gmail_remove_dots : false } ) ;
139+
140+ User . findById ( req . user . id , ( err , user ) => {
141+ if ( err ) { return next ( err ) ; }
142+ if ( user . email !== req . body . email ) user . emailVerified = false ;
143+ user . email = req . body . email || '' ;
144+ user . name = req . body . name || '' ;
145+ user . gender = req . body . gender || '' ;
146+ user . save ( ( err ) => {
147+ if ( err ) {
148+ if ( err . code === 11000 ) {
149+ req . flash ( 'errors' , { msg : 'The email address you have entered is already associated with an account.' } ) ;
150+ return res . redirect ( '/account' ) ;
151+ }
152+ return next ( err ) ;
153+ }
154+ req . flash ( 'success' , { msg : 'Profile information has been updated.' } ) ;
155+ res . redirect ( '/account' ) ;
156+ } ) ;
157+ } ) ;
158+ } ;
159+
160+ /**
161+ * POST /account/password
162+ * Update current password.
163+ */
164+ exports . postUpdatePassword = ( req , res , next ) => {
165+ const validationErrors = [ ] ;
166+ if ( ! validator . isLength ( req . body . password , { min : 8 } ) ) validationErrors . push ( { msg : 'Password must be at least 8 characters long' } ) ;
167+ if ( req . body . password !== req . body . confirmPassword ) validationErrors . push ( { msg : 'Passwords do not match' } ) ;
168+
169+ if ( validationErrors . length ) {
170+ req . flash ( 'errors' , validationErrors ) ;
171+ return res . redirect ( '/account' ) ;
172+ }
173+
174+ User . findById ( req . user . id , ( err , user ) => {
175+ if ( err ) { return next ( err ) ; }
176+ user . password = req . body . password ;
177+ user . save ( ( err ) => {
178+ if ( err ) { return next ( err ) ; }
179+ req . flash ( 'success' , { msg : 'Password has been changed.' } ) ;
180+ res . redirect ( '/account' ) ;
181+ } ) ;
182+ } ) ;
183+ } ;
184+
185+ /**
186+ * POST /account/delete
187+ * Delete user account.
188+ */
189+ exports . postDeleteAccount = ( req , res , next ) => {
190+ User . deleteOne ( { _id : req . user . id } , ( err ) => {
191+ if ( err ) { return next ( err ) ; }
192+ req . logout ( ) ;
193+ req . flash ( 'info' , { msg : 'Your account has been deleted.' } ) ;
194+ res . redirect ( '/' ) ;
195+ } ) ;
196+ } ;
0 commit comments