11import { PayloadRequest } from "payload" ;
22import { OAuth2Plugin , defaultGetToken } from "payload-oauth2" ;
33
4- ////////////////////////////////////////////////////////////////////////////////
5- // Google OAuth
6- ////////////////////////////////////////////////////////////////////////////////
74export const googleOAuth = OAuth2Plugin ( {
85 enabled :
96 typeof process . env . GOOGLE_CLIENT_ID === "string" &&
107 typeof process . env . GOOGLE_CLIENT_SECRET === "string" ,
118 strategyName : "google" ,
129 useEmailAsIdentity : true ,
13- serverURL : process . env . NEXT_PUBLIC_URL || "http://localhost:3000 " ,
10+ serverURL : process . env . NEXT_PUBLIC_URL || "http://localhost:8000 " ,
1411 clientId : process . env . GOOGLE_CLIENT_ID || "" ,
1512 clientSecret : process . env . GOOGLE_CLIENT_SECRET || "" ,
1613 authorizePath : "/oauth/google" ,
@@ -23,44 +20,98 @@ export const googleOAuth = OAuth2Plugin({
2320 "https://www.googleapis.com/auth/userinfo.profile" ,
2421 ] ,
2522 providerAuthorizationUrl : "https://accounts.google.com/o/oauth2/v2/auth" ,
26- getUserInfo : async ( accessToken : string , req : PayloadRequest ) => {
27- const response = await fetch (
28- "https://www.googleapis.com/oauth2/v3/userinfo" ,
29- { headers : { Authorization : `Bearer ${ accessToken } ` } } ,
30- ) ;
31- const user = await response . json ( ) ;
32- return { email : user . email , sub : user . sub } ;
33- } ,
34- /**
35- * This param is optional to demonstrate how to customize your own
36- * `getToken` function (i.e. add hooks to run after getting the token)
37- * Leave this blank should you wish to use the default getToken function
38- */
23+
24+ getUserInfo : async ( accessToken : string , req : PayloadRequest ) => {
25+ const response = await fetch (
26+ // https://cloud.google.com/identity-platform/docs/reference/rest/v1/UserInfo
27+ "https://www.googleapis.com/oauth2/v3/userinfo" ,
28+ { headers : { Authorization : `Bearer ${ accessToken } ` } }
29+ ) ;
30+
31+ const user = await response . json ( ) ;
32+
33+ // const existingUser = await req.payload.find({
34+ // collection: 'users',
35+ // where: {
36+ // email: {
37+ // equals: req.user?.email,
38+ // },
39+ // },
40+ // limit: 1,
41+ // })
42+
43+ // if (!existingUser.docs || existingUser.docs.length === 0) {
44+ // console.log('Creating new user for:', req.user)
45+
46+ // const newUser = await req.payload.create({
47+ // collection: 'users',
48+ // data: {
49+ // email: req.user?.email,
50+ // displayName: req.user?.displayName,
51+ // mediaUrl: req.user?.photoUrl
52+ // // roles: ['hacker'],
53+ // },
54+ // })
55+ // return {
56+ // ...newUser,
57+ // // roles: ['customer'],
58+ // }
59+ // }
60+
61+ // console.log('Found existing user:', existingUser.docs[0])
62+ // const userFromDB = existingUser.docs[0]
63+
64+ return {
65+ email : user . email ,
66+ displayName : user . name ,
67+ firstName : user . given_name ,
68+ lastName : user . family_name ,
69+ mediaUrl : user . photoUrl ,
70+ googleSub : user . sub ,
71+ googleEmailVerified : user . email_verified ,
72+ } ;
73+ } ,
3974 getToken : async ( code : string , req : PayloadRequest ) => {
40- const redirectUri = `${ process . env . NEXT_PUBLIC_URL || "http://localhost:3000 " } /api/users/oauth/google/callback` ;
75+ const redirectUri = `${ process . env . NEXT_PUBLIC_URL || "http://localhost:8000 " } /api/users/oauth/google/callback` ;
4176 const token = await defaultGetToken (
4277 "https://oauth2.googleapis.com/token" ,
4378 process . env . GOOGLE_CLIENT_ID || "" ,
4479 process . env . GOOGLE_CLIENT_SECRET || "" ,
4580 redirectUri ,
4681 code ,
4782 ) ;
83+
4884 ////////////////////////////////////////////////////////////////////////////
4985 // Consider this section afterToken hook
5086 ////////////////////////////////////////////////////////////////////////////
51- req . payload . logger . info ( "Received token: ${token} 👀" ) ;
87+ // req.payload.logger.info(`Received token: ${token} 👀`);
88+
5289 if ( req . user ) {
5390 req . payload . update ( {
5491 collection : "users" ,
55- id : req . user . id ,
56- data : { } ,
92+ data : {
93+ email : req . user ?. email ,
94+ firstName : req . user . given_name ,
95+ lastName : req . user . family_name ,
96+ mediaUrl : req . user . photoUrl ,
97+ googleSub : req . user . sub ,
98+ googleEmailVerified : req . user . email_verified ,
99+ } ,
57100 } ) ;
58- }
101+ } ;
59102
60103 return token ;
61104 } ,
62- successRedirect : ( req ) => {
105+
106+ successRedirect : ( req : PayloadRequest , accessToken ?: string ) => {
63107 return "/admin" ;
108+ // const user = req.user
109+ // if (user && Array.isArray(user.roles)) {
110+ // if (user.roles.includes('admin')) {
111+ // return '/admin'
112+ // }
113+ // }
114+ // return '/' // Default redirect for customers
64115 } ,
65116 failureRedirect : ( req , err ) => {
66117 req . payload . logger . error ( err ) ;
0 commit comments