1
- const jwt_decode = require ( ' jwt-decode' ) ;
1
+ const jwt_decode = require ( " jwt-decode" ) ;
2
2
3
3
const ADMIN_ROLES = {
4
-
5
- GENSEC_SCITECH :
' [email protected] ' ,
6
- GENSEC_ACADEMIC :
' [email protected] ' ,
7
- GENSEC_CULTURAL :
' [email protected] ' ,
8
- GENSEC_SPORTS :
' [email protected] ' ,
4
+
5
+ GENSEC_SCITECH :
" [email protected] " ,
6
+ GENSEC_ACADEMIC :
" [email protected] " ,
7
+ GENSEC_CULTURAL :
" [email protected] " ,
8
+ GENSEC_SPORTS :
" [email protected] " ,
9
9
} ;
10
10
11
11
const ADMIN_CREDENTIALS = {
@@ -37,29 +37,33 @@ const authenticateAdmin = (req, res, next, expectedRole) => {
37
37
const decoded = jwt_decode ( jwtToken ) ;
38
38
39
39
if ( ! jwtToken || ! isAdmin ( decoded , expectedRole ) ) {
40
- return res . status ( 401 ) . json ( { success : false , message : 'Unauthorized Admin' } ) ;
40
+ return res
41
+ . status ( 401 )
42
+ . json ( { success : false , message : "Unauthorized Admin" } ) ;
41
43
}
42
44
43
45
req . DB_credentials = ADMIN_CREDENTIALS [ expectedRole ] ;
44
46
req . decoded = decoded ;
45
47
next ( ) ;
46
48
} catch ( error ) {
47
49
console . error ( error ) ;
48
- return res . status ( 500 ) . json ( { success : false , message : 'Internal Server Error' } ) ;
50
+ return res
51
+ . status ( 500 )
52
+ . json ( { success : false , message : "Internal Server Error" } ) ;
49
53
}
50
54
} ;
51
55
52
56
const isAdmin = ( decoded , expectedRole ) => {
53
57
return (
54
58
decoded . email === ADMIN_ROLES [ expectedRole ] &&
55
- decoded . iss === ' https://accounts.google.com' &&
59
+ decoded . iss === " https://accounts.google.com" &&
56
60
decoded . exp > Date . now ( ) / 1000 &&
57
61
decoded . aud === process . env . GOOGLE_CLIENT_ID
58
62
) ;
59
63
} ;
60
64
61
65
exports . restrictToPresident = ( req , res , next ) => {
62
- authenticateAdmin ( req , res , next , ' PRESIDENT' ) ;
66
+ authenticateAdmin ( req , res , next , " PRESIDENT" ) ;
63
67
} ;
64
68
65
69
exports . restrictToAdmin = ( req , res , next ) => {
@@ -71,22 +75,33 @@ const getAdminRole = (req) => {
71
75
const userEmail = decoded . email ;
72
76
73
77
if ( userEmail === ADMIN_ROLES . GENSEC_SCITECH ) {
74
- return ' GENSEC_SCITECH' ;
78
+ return " GENSEC_SCITECH" ;
75
79
} else if ( userEmail === ADMIN_ROLES . GENSEC_ACADEMIC ) {
76
- return ' GENSEC_ACADEMIC' ;
80
+ return " GENSEC_ACADEMIC" ;
77
81
} else if ( userEmail === ADMIN_ROLES . GENSEC_CULTURAL ) {
78
- return ' GENSEC_CULTURAL' ;
82
+ return " GENSEC_CULTURAL" ;
79
83
} else if ( userEmail === ADMIN_ROLES . GENSEC_SPORTS ) {
80
- return ' GENSEC_SPORTS' ;
84
+ return " GENSEC_SPORTS" ;
81
85
}
82
86
83
- return '' ; // Default case or handle as needed
87
+ return "" ; // Default case or handle as needed
84
88
} ;
85
89
86
90
exports . isAuthenticated = ( req , res , next ) => {
87
91
if ( req . isAuthenticated ( ) ) {
88
92
return next ( ) ;
89
93
} else {
90
- return res . status ( 401 ) . json ( { message : ' Unauthorized' } ) ;
94
+ return res . status ( 401 ) . json ( { message : " Unauthorized" } ) ;
91
95
}
92
96
} ;
97
+
98
+ exports . handleBadRequests = function ( err , req , res ) {
99
+ console . error ( err . stack ) ;
100
+ res . status ( 500 ) . send ( "Something broke!" ) ;
101
+ } ;
102
+
103
+ exports . exceptionHandler = function ( f ) {
104
+ return function ( req , res , next ) {
105
+ Promise . resolve ( f ( req , res , next ) ) . catch ( next ) ;
106
+ } ;
107
+ } ;
0 commit comments