@@ -2,142 +2,142 @@ module.exports = function(app, passport) {
2
2
3
3
// normal routes ===============================================================
4
4
5
- // show the home page (will also have our login links)
6
- app . get ( '/' , function ( req , res ) {
7
- res . render ( 'index.ejs' ) ;
8
- } ) ;
9
-
10
- // PROFILE SECTION =========================
11
- app . get ( '/profile' , isLoggedIn , function ( req , res ) {
12
- res . render ( 'profile.ejs' , {
13
- user : req . user
14
- } ) ;
15
- } ) ;
16
-
17
- // LOGOUT ==============================
18
- app . get ( '/logout' , function ( req , res ) {
19
- req . logout ( ) ;
20
- res . redirect ( '/' ) ;
21
- } ) ;
5
+ // show the home page (will also have our login links)
6
+ app . get ( '/' , function ( req , res ) {
7
+ res . render ( 'index.ejs' ) ;
8
+ } ) ;
9
+
10
+ // PROFILE SECTION =========================
11
+ app . get ( '/profile' , isLoggedIn , function ( req , res ) {
12
+ res . render ( 'profile.ejs' , {
13
+ user : req . user
14
+ } ) ;
15
+ } ) ;
16
+
17
+ // LOGOUT ==============================
18
+ app . get ( '/logout' , function ( req , res ) {
19
+ req . logout ( ) ;
20
+ res . redirect ( '/' ) ;
21
+ } ) ;
22
22
23
23
// =============================================================================
24
24
// AUTHENTICATE (FIRST LOGIN) ==================================================
25
25
// =============================================================================
26
26
27
- // locally --------------------------------
28
- // LOGIN ===============================
29
- // show the login form
30
- app . get ( '/login' , function ( req , res ) {
31
- res . render ( 'login.ejs' , { message : req . flash ( 'loginMessage' ) } ) ;
32
- } ) ;
33
-
34
- // process the login form
35
- app . post ( '/login' , passport . authenticate ( 'local-login' , {
36
- successRedirect : '/profile' , // redirect to the secure profile section
37
- failureRedirect : '/login' , // redirect back to the signup page if there is an error
38
- failureFlash : true // allow flash messages
39
- } ) ) ;
40
-
41
- // SIGNUP =================================
42
- // show the signup form
43
- app . get ( '/signup' , function ( req , res ) {
44
- res . render ( 'signup.ejs' , { message : req . flash ( 'signupMessage' ) } ) ;
45
- } ) ;
46
-
47
- // process the signup form
48
- app . post ( '/signup' , passport . authenticate ( 'local-signup' , {
49
- successRedirect : '/profile' , // redirect to the secure profile section
50
- failureRedirect : '/signup' , // redirect back to the signup page if there is an error
51
- failureFlash : true // allow flash messages
52
- } ) ) ;
53
-
54
- // facebook -------------------------------
55
-
56
- // send to facebook to do the authentication
57
- app . get ( '/auth/facebook' , passport . authenticate ( 'facebook' , { scope : 'email' } ) ) ;
58
-
59
- // handle the callback after facebook has authenticated the user
60
- app . get ( '/auth/facebook/callback' ,
61
- passport . authenticate ( 'facebook' , {
62
- successRedirect : '/profile' ,
63
- failureRedirect : '/'
64
- } ) ) ;
65
-
66
- // twitter --------------------------------
67
-
68
- // send to twitter to do the authentication
69
- app . get ( '/auth/twitter' , passport . authenticate ( 'twitter' , { scope : 'email' } ) ) ;
70
-
71
- // handle the callback after twitter has authenticated the user
72
- app . get ( '/auth/twitter/callback' ,
73
- passport . authenticate ( 'twitter' , {
74
- successRedirect : '/profile' ,
75
- failureRedirect : '/'
76
- } ) ) ;
77
-
78
-
79
- // google ---------------------------------
80
-
81
- // send to google to do the authentication
82
- app . get ( '/auth/google' , passport . authenticate ( 'google' , { scope : [ 'profile' , 'email' ] } ) ) ;
83
-
84
- // the callback after google has authenticated the user
85
- app . get ( '/auth/google/callback' ,
86
- passport . authenticate ( 'google' , {
87
- successRedirect : '/profile' ,
88
- failureRedirect : '/'
89
- } ) ) ;
27
+ // locally --------------------------------
28
+ // LOGIN ===============================
29
+ // show the login form
30
+ app . get ( '/login' , function ( req , res ) {
31
+ res . render ( 'login.ejs' , { message : req . flash ( 'loginMessage' ) } ) ;
32
+ } ) ;
33
+
34
+ // process the login form
35
+ app . post ( '/login' , passport . authenticate ( 'local-login' , {
36
+ successRedirect : '/profile' , // redirect to the secure profile section
37
+ failureRedirect : '/login' , // redirect back to the signup page if there is an error
38
+ failureFlash : true // allow flash messages
39
+ } ) ) ;
40
+
41
+ // SIGNUP =================================
42
+ // show the signup form
43
+ app . get ( '/signup' , function ( req , res ) {
44
+ res . render ( 'signup.ejs' , { message : req . flash ( 'signupMessage' ) } ) ;
45
+ } ) ;
46
+
47
+ // process the signup form
48
+ app . post ( '/signup' , passport . authenticate ( 'local-signup' , {
49
+ successRedirect : '/profile' , // redirect to the secure profile section
50
+ failureRedirect : '/signup' , // redirect back to the signup page if there is an error
51
+ failureFlash : true // allow flash messages
52
+ } ) ) ;
53
+
54
+ // facebook -------------------------------
55
+
56
+ // send to facebook to do the authentication
57
+ app . get ( '/auth/facebook' , passport . authenticate ( 'facebook' , { scope : 'email' } ) ) ;
58
+
59
+ // handle the callback after facebook has authenticated the user
60
+ app . get ( '/auth/facebook/callback' ,
61
+ passport . authenticate ( 'facebook' , {
62
+ successRedirect : '/profile' ,
63
+ failureRedirect : '/'
64
+ } ) ) ;
65
+
66
+ // twitter --------------------------------
67
+
68
+ // send to twitter to do the authentication
69
+ app . get ( '/auth/twitter' , passport . authenticate ( 'twitter' , { scope : 'email' } ) ) ;
70
+
71
+ // handle the callback after twitter has authenticated the user
72
+ app . get ( '/auth/twitter/callback' ,
73
+ passport . authenticate ( 'twitter' , {
74
+ successRedirect : '/profile' ,
75
+ failureRedirect : '/'
76
+ } ) ) ;
77
+
78
+
79
+ // google ---------------------------------
80
+
81
+ // send to google to do the authentication
82
+ app . get ( '/auth/google' , passport . authenticate ( 'google' , { scope : [ 'profile' , 'email' ] } ) ) ;
83
+
84
+ // the callback after google has authenticated the user
85
+ app . get ( '/auth/google/callback' ,
86
+ passport . authenticate ( 'google' , {
87
+ successRedirect : '/profile' ,
88
+ failureRedirect : '/'
89
+ } ) ) ;
90
90
91
91
// =============================================================================
92
92
// AUTHORIZE (ALREADY LOGGED IN / CONNECTING OTHER SOCIAL ACCOUNT) =============
93
93
// =============================================================================
94
94
95
- // locally --------------------------------
96
- app . get ( '/connect/local' , function ( req , res ) {
97
- res . render ( 'connect-local.ejs' , { message : req . flash ( 'loginMessage' ) } ) ;
98
- } ) ;
99
- app . post ( '/connect/local' , passport . authenticate ( 'local-signup' , {
100
- successRedirect : '/profile' , // redirect to the secure profile section
101
- failureRedirect : '/connect/local' , // redirect back to the signup page if there is an error
102
- failureFlash : true // allow flash messages
103
- } ) ) ;
95
+ // locally --------------------------------
96
+ app . get ( '/connect/local' , function ( req , res ) {
97
+ res . render ( 'connect-local.ejs' , { message : req . flash ( 'loginMessage' ) } ) ;
98
+ } ) ;
99
+ app . post ( '/connect/local' , passport . authenticate ( 'local-signup' , {
100
+ successRedirect : '/profile' , // redirect to the secure profile section
101
+ failureRedirect : '/connect/local' , // redirect back to the signup page if there is an error
102
+ failureFlash : true // allow flash messages
103
+ } ) ) ;
104
104
105
- // facebook -------------------------------
105
+ // facebook -------------------------------
106
106
107
- // send to facebook to do the authentication
108
- app . get ( '/connect/facebook' , passport . authorize ( 'facebook' , { scope : 'email' } ) ) ;
107
+ // send to facebook to do the authentication
108
+ app . get ( '/connect/facebook' , passport . authorize ( 'facebook' , { scope : 'email' } ) ) ;
109
109
110
- // handle the callback after facebook has authorized the user
111
- app . get ( '/connect/facebook/callback' ,
112
- passport . authorize ( 'facebook' , {
113
- successRedirect : '/profile' ,
114
- failureRedirect : '/'
115
- } ) ) ;
110
+ // handle the callback after facebook has authorized the user
111
+ app . get ( '/connect/facebook/callback' ,
112
+ passport . authorize ( 'facebook' , {
113
+ successRedirect : '/profile' ,
114
+ failureRedirect : '/'
115
+ } ) ) ;
116
116
117
- // twitter --------------------------------
117
+ // twitter --------------------------------
118
118
119
- // send to twitter to do the authentication
120
- app . get ( '/connect/twitter' , passport . authorize ( 'twitter' , { scope : 'email' } ) ) ;
119
+ // send to twitter to do the authentication
120
+ app . get ( '/connect/twitter' , passport . authorize ( 'twitter' , { scope : 'email' } ) ) ;
121
121
122
- // handle the callback after twitter has authorized the user
123
- app . get ( '/connect/twitter/callback' ,
124
- passport . authorize ( 'twitter' , {
125
- successRedirect : '/profile' ,
126
- failureRedirect : '/'
127
- } ) ) ;
122
+ // handle the callback after twitter has authorized the user
123
+ app . get ( '/connect/twitter/callback' ,
124
+ passport . authorize ( 'twitter' , {
125
+ successRedirect : '/profile' ,
126
+ failureRedirect : '/'
127
+ } ) ) ;
128
128
129
129
130
- // google ---------------------------------
130
+ // google ---------------------------------
131
131
132
- // send to google to do the authentication
133
- app . get ( '/connect/google' , passport . authorize ( 'google' , { scope : [ 'profile' , 'email' ] } ) ) ;
132
+ // send to google to do the authentication
133
+ app . get ( '/connect/google' , passport . authorize ( 'google' , { scope : [ 'profile' , 'email' ] } ) ) ;
134
134
135
- // the callback after google has authorized the user
136
- app . get ( '/connect/google/callback' ,
137
- passport . authorize ( 'google' , {
138
- successRedirect : '/profile' ,
139
- failureRedirect : '/'
140
- } ) ) ;
135
+ // the callback after google has authorized the user
136
+ app . get ( '/connect/google/callback' ,
137
+ passport . authorize ( 'google' , {
138
+ successRedirect : '/profile' ,
139
+ failureRedirect : '/'
140
+ } ) ) ;
141
141
142
142
// =============================================================================
143
143
// UNLINK ACCOUNTS =============================================================
@@ -146,50 +146,50 @@ module.exports = function(app, passport) {
146
146
// for local account, remove email and password
147
147
// user account will stay active in case they want to reconnect in the future
148
148
149
- // local -----------------------------------
150
- app . get ( '/unlink/local' , isLoggedIn , function ( req , res ) {
151
- var user = req . user ;
152
- user . local . email = undefined ;
153
- user . local . password = undefined ;
154
- user . save ( function ( err ) {
155
- res . redirect ( '/profile' ) ;
156
- } ) ;
157
- } ) ;
158
-
159
- // facebook -------------------------------
160
- app . get ( '/unlink/facebook' , isLoggedIn , function ( req , res ) {
161
- var user = req . user ;
162
- user . facebook . token = undefined ;
163
- user . save ( function ( err ) {
164
- res . redirect ( '/profile' ) ;
165
- } ) ;
166
- } ) ;
167
-
168
- // twitter --------------------------------
169
- app . get ( '/unlink/twitter' , isLoggedIn , function ( req , res ) {
170
- var user = req . user ;
171
- user . twitter . token = undefined ;
172
- user . save ( function ( err ) {
173
- res . redirect ( '/profile' ) ;
174
- } ) ;
175
- } ) ;
176
-
177
- // google ---------------------------------
178
- app . get ( '/unlink/google' , isLoggedIn , function ( req , res ) {
179
- var user = req . user ;
180
- user . google . token = undefined ;
181
- user . save ( function ( err ) {
182
- res . redirect ( '/profile' ) ;
183
- } ) ;
184
- } ) ;
149
+ // local -----------------------------------
150
+ app . get ( '/unlink/local' , isLoggedIn , function ( req , res ) {
151
+ var user = req . user ;
152
+ user . local . email = undefined ;
153
+ user . local . password = undefined ;
154
+ user . save ( function ( err ) {
155
+ res . redirect ( '/profile' ) ;
156
+ } ) ;
157
+ } ) ;
158
+
159
+ // facebook -------------------------------
160
+ app . get ( '/unlink/facebook' , isLoggedIn , function ( req , res ) {
161
+ var user = req . user ;
162
+ user . facebook . token = undefined ;
163
+ user . save ( function ( err ) {
164
+ res . redirect ( '/profile' ) ;
165
+ } ) ;
166
+ } ) ;
167
+
168
+ // twitter --------------------------------
169
+ app . get ( '/unlink/twitter' , isLoggedIn , function ( req , res ) {
170
+ var user = req . user ;
171
+ user . twitter . token = undefined ;
172
+ user . save ( function ( err ) {
173
+ res . redirect ( '/profile' ) ;
174
+ } ) ;
175
+ } ) ;
176
+
177
+ // google ---------------------------------
178
+ app . get ( '/unlink/google' , isLoggedIn , function ( req , res ) {
179
+ var user = req . user ;
180
+ user . google . token = undefined ;
181
+ user . save ( function ( err ) {
182
+ res . redirect ( '/profile' ) ;
183
+ } ) ;
184
+ } ) ;
185
185
186
186
187
187
} ;
188
188
189
189
// route middleware to ensure user is logged in
190
190
function isLoggedIn ( req , res , next ) {
191
- if ( req . isAuthenticated ( ) )
192
- return next ( ) ;
191
+ if ( req . isAuthenticated ( ) )
192
+ return next ( ) ;
193
193
194
- res . redirect ( '/' ) ;
194
+ res . redirect ( '/' ) ;
195
195
}
0 commit comments