@@ -3,38 +3,19 @@ import passport from 'passport';
3
3
4
4
const router = new Router ( ) ;
5
5
6
- router . get ( '/auth/github' , passport . authenticate ( 'github' ) ) ;
7
- router . get ( '/auth/github/callback' , ( req , res , next ) => {
6
+ const authenticateOAuth = ( service ) => ( req , res , next ) => {
8
7
passport . authenticate (
9
- 'github' ,
8
+ service ,
10
9
{ failureRedirect : '/login' } ,
11
- ( err , user ) => {
10
+ ( err , user , info ) => {
12
11
if ( err ) {
13
12
// use query string param to show error;
14
- res . redirect ( ' /account?error=github' ) ;
13
+ res . redirect ( ` /account?error=${ service } ` ) ;
15
14
return ;
16
15
}
17
16
18
- req . logIn ( user , ( loginErr ) => {
19
- if ( loginErr ) {
20
- next ( loginErr ) ;
21
- return ;
22
- }
23
- res . redirect ( '/' ) ;
24
- } ) ;
25
- }
26
- ) ( req , res , next ) ;
27
- } ) ;
28
-
29
- router . get ( '/auth/google' , passport . authenticate ( 'google' ) ) ;
30
- router . get ( '/auth/google/callback' , ( req , res , next ) => {
31
- passport . authenticate (
32
- 'google' ,
33
- { failureRedirect : '/login' } ,
34
- ( err , user ) => {
35
- if ( err ) {
36
- // use query string param to show error;
37
- res . redirect ( '/account?error=google' ) ;
17
+ if ( ! user ) {
18
+ res . redirect ( `/account?error=${ service } NoUser` ) ;
38
19
return ;
39
20
}
40
21
@@ -47,6 +28,12 @@ router.get('/auth/google/callback', (req, res, next) => {
47
28
} ) ;
48
29
}
49
30
) ( req , res , next ) ;
50
- } ) ;
31
+ } ;
32
+
33
+ router . get ( '/auth/github' , passport . authenticate ( 'github' ) ) ;
34
+ router . get ( '/auth/github/callback' , authenticateOAuth ( 'github' ) ) ;
35
+
36
+ router . get ( '/auth/google' , passport . authenticate ( 'google' ) ) ;
37
+ router . get ( '/auth/google/callback' , authenticateOAuth ( 'google' ) ) ;
51
38
52
39
export default router ;
0 commit comments