9
9
USER_DOES_NOT_EXIST_ERROR ,
10
10
} = require ( "../constants/errorMessages" ) ;
11
11
12
+ const googleAuthLogin = ( req , res , next ) => {
13
+ const { redirectURL } = req . query ;
14
+ return passport . authenticate ( "google" , {
15
+ scope : [ "email" ] ,
16
+ state : redirectURL ,
17
+ } ) ( req , res , next ) ;
18
+ } ;
19
+
20
+ const googleAuthCallback = ( req , res , next ) => {
21
+ const rdsUiUrl = new URL ( config . get ( "services.rdsUi.baseUrl" ) ) ;
22
+ let authRedirectionUrl = rdsUiUrl ;
23
+
24
+ if ( "state" in req . query ) {
25
+ try {
26
+ const redirectUrl = new URL ( req . query . state ) ;
27
+
28
+ if ( `.${ redirectUrl . hostname } ` . endsWith ( `.${ rdsUiUrl . hostname } ` ) ) {
29
+ // Matching *.realdevsquad.com
30
+ authRedirectionUrl = redirectUrl ;
31
+ // console.log("redirect url is", authRedirectionUrl);
32
+ // devMode = Boolean(redirectUrl.searchParams.get("dev"));
33
+ } else {
34
+ logger . error ( `Malicious redirect URL provided URL: ${ redirectUrl } , Will redirect to RDS` ) ;
35
+ }
36
+ } catch ( error ) {
37
+ logger . error ( "Invalid redirect URL provided" , error ) ;
38
+ }
39
+ }
40
+ try {
41
+ return passport . authenticate ( "google" , { session : false } , async ( err , accessToken , user ) => {
42
+ if ( err ) {
43
+ logger . error ( err ) ;
44
+ return res . boom . unauthorized ( "User cannot be authenticated" ) ;
45
+ }
46
+ // console.log("user", user);
47
+ const userData = {
48
+ email : user . emails [ 0 ] . value ,
49
+ created_at : Date . now ( ) ,
50
+ updated_at : null ,
51
+ } ;
52
+ // console.log("userData", userData);
53
+
54
+ const userDataFromDB = await users . fetchUser ( { email : userData . email } ) ;
55
+ // console.log("userDataFromDB", userDataFromDB);
56
+
57
+ if ( userDataFromDB . userExists ) {
58
+ if ( userDataFromDB . user . roles . developer === true ) {
59
+ // console.log("hi");
60
+ return res . boom . unauthorized ( "User is not allowed to login via Google" ) ;
61
+ }
62
+ }
63
+
64
+ const { userId, incompleteUserDetails } = await users . addOrUpdate ( userData ) ;
65
+ // console.log(role);
66
+
67
+ const token = authService . generateAuthToken ( { userId } ) ;
68
+
69
+ const cookieOptions = {
70
+ domain : rdsUiUrl . hostname ,
71
+ expires : new Date ( Date . now ( ) + config . get ( "userToken.ttl" ) * 1000 ) ,
72
+ httpOnly : true ,
73
+ secure : true ,
74
+ sameSite : "lax" ,
75
+ } ;
76
+
77
+ res . cookie ( config . get ( "userToken.cookieName" ) , token , cookieOptions ) ;
78
+
79
+ if ( incompleteUserDetails ) authRedirectionUrl = "https://my.realdevsquad.com/new-signup" ;
80
+
81
+ return res . redirect ( authRedirectionUrl ) ;
82
+ } ) ( req , res , next ) ;
83
+ } catch ( err ) {
84
+ logger . error ( err ) ;
85
+ return res . boom . unauthorized ( "User cannot be authenticated" ) ;
86
+ }
87
+ } ;
88
+
12
89
/**
13
90
* Makes authentication call to GitHub statergy
14
91
*
@@ -56,7 +133,6 @@ const githubAuthCallback = (req, res, next) => {
56
133
}
57
134
58
135
if ( redirectUrl . searchParams . get ( "v2" ) === "true" ) isV2FlagPresent = true ;
59
-
60
136
if ( `.${ redirectUrl . hostname } ` . endsWith ( `.${ rdsUiUrl . hostname } ` ) ) {
61
137
// Matching *.realdevsquad.com
62
138
authRedirectionUrl = redirectUrl ;
@@ -74,14 +150,38 @@ const githubAuthCallback = (req, res, next) => {
74
150
logger . error ( err ) ;
75
151
return res . boom . unauthorized ( "User cannot be authenticated" ) ;
76
152
}
153
+ // console.log(accessToken);
154
+ // console.log("user", user);
77
155
userData = {
78
156
github_id : user . username ,
79
157
github_display_name : user . displayName ,
158
+ email : user . _json . email ,
80
159
github_created_at : Number ( new Date ( user . _json . created_at ) . getTime ( ) ) ,
81
160
github_user_id : user . id ,
82
161
created_at : Date . now ( ) ,
83
162
updated_at : null ,
84
163
} ;
164
+ // console.log(userData);
165
+
166
+ if ( userData . email === null ) {
167
+ const res = await fetch ( "https://api.github.com/user/emails" , {
168
+ headers : {
169
+ Authorization : `token ${ accessToken } ` ,
170
+ } ,
171
+ } ) ;
172
+ const emails = await res . json ( ) ;
173
+ // console.log("emails", emails);
174
+ const primaryEmails = emails . filter ( ( item ) => item . primary ) ;
175
+ // console.log("primaryEmails", primaryEmails);
176
+
177
+ // Get the first primary email, if it exists
178
+ if ( primaryEmails . length > 0 ) {
179
+ userData . email = primaryEmails [ 0 ] . email ;
180
+ } else {
181
+ userData . email = null ;
182
+ // console.log("userData.email", userData.email);
183
+ }
184
+ }
85
185
86
186
const { userId, incompleteUserDetails, role } = await users . addOrUpdate ( userData ) ;
87
187
@@ -232,6 +332,8 @@ const fetchDeviceDetails = async (req, res) => {
232
332
module . exports = {
233
333
githubAuthLogin,
234
334
githubAuthCallback,
335
+ googleAuthLogin,
336
+ googleAuthCallback,
235
337
signout,
236
338
storeUserDeviceInfo,
237
339
updateAuthStatus,
0 commit comments