@@ -4,9 +4,7 @@ const serverUrl = process.env.SERVER_URL;
4
4
export default async function gooogleauth ( request , response ) {
5
5
const code = request . body . code ;
6
6
const baseUrl = new URL ( process . env . SERVER_URL ) ;
7
- // const sessiontoken = request.headers;
8
- // console.log('sessiontoken', sessiontoken);
9
- // console.log('google code', code);
7
+
10
8
try {
11
9
const userRes = await axios . get ( serverUrl + '/users/me' , {
12
10
headers : {
@@ -16,9 +14,10 @@ export default async function gooogleauth(request, response) {
16
14
} ) ;
17
15
const userId = userRes . data && userRes . data . objectId ;
18
16
if ( userId ) {
19
- const clientId = process . env . GOOGLE_CLIENT_ID ; // '918704711393-thhv3re2pfqvve76tgb86ulu1tlpssrk.apps.googleusercontent.com';
20
- const clientSecret = process . env . GOOGLE_CLIENT_SECRET ; //'3NqyXVNm4jUhwNE4D8eVosII';
21
- const redirectUri = baseUrl . origin || 'http://localhost:3000' ; // Should match the redirect URI used in the authorization request
17
+ const clientId = process . env . GOOGLE_CLIENT_ID ;
18
+ const clientSecret = process . env . GOOGLE_CLIENT_SECRET ;
19
+ const redirectUri =
20
+ baseUrl ?. hostname === 'localhost' ? 'http://localhost:3000' : baseUrl . origin ; // Should match the redirect URI used in the authorization request
22
21
const tokenEndpoint = 'https://oauth2.googleapis.com/token' ;
23
22
24
23
const params = new URLSearchParams ( ) ;
@@ -42,12 +41,16 @@ export default async function gooogleauth(request, response) {
42
41
const updateExtUser = await extUserCls . save ( null , { useMasterKey : true } ) ;
43
42
// console.log('updateExtUser ', updateExtUser);
44
43
}
45
- return response . status ( 200 ) . json ( { status : 'success! ' } ) ;
44
+ return response . status ( 200 ) . json ( { status : 'success' } ) ;
46
45
} else {
47
46
return response . status ( 404 ) . json ( { message : 'user not found!' } ) ;
48
47
}
49
48
} catch ( err ) {
50
- console . log ( 'err in google auth' , err . message ) ;
51
- return response . status ( 404 ) . json ( { message : err . message } ) ;
49
+ console . log ( 'err in google auth' , err ) ;
50
+ if ( err ?. response ?. data ?. error ) {
51
+ return response . status ( 404 ) . json ( { message : err . response . data . error } ) ;
52
+ } else {
53
+ return response . status ( 404 ) . json ( { message : err . message } ) ;
54
+ }
52
55
}
53
56
}
0 commit comments