Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit c8b77aa

Browse files
committed
feat(axiom): implement google auth
1 parent 449a51f commit c8b77aa

File tree

4 files changed

+116
-28
lines changed

4 files changed

+116
-28
lines changed

apps/axiom/src/payload.config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
// github.com/jhb-software/payload-plugins/tree/main/geocoding
33
import path from "node:path";
44
// import { s3Storage } from '@payloadcms/storage-s3'
5-
// import {linkedinOAuth} from './endpoints/auth/linkedin'
65
/* eslint-disable node/prefer-global/process */
76
import { fileURLToPath } from "node:url";
8-
// import { googleOAuth } from '@cuhacking/cms/endpoints/auth/google'
7+
import { googleOAuth } from '@/cms/endpoints/auth/google'
98
import {Brands} from "@/db/collections"
109
import { Media, Users, Emails } from "@/db/collections/models";
1110
import {Website, SocialLinks} from "@/db/collections/globals";
@@ -119,7 +118,7 @@ export default buildConfig({
119118
// endpoint: process.env.S3_ENDPOINT,
120119
// },
121120
// }),
122-
// googleOAuth,
121+
googleOAuth,
123122
// linkedinOAuth
124123
// payloadCloudPlugin(),
125124
],

libs/cms/endpoints/auth/google.ts

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { PayloadRequest } from "payload";
22
import { OAuth2Plugin, defaultGetToken } from "payload-oauth2";
33

4-
////////////////////////////////////////////////////////////////////////////////
5-
// Google OAuth
6-
////////////////////////////////////////////////////////////////////////////////
74
export 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);

libs/cms/endpoints/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './auth'

libs/db/collections/models/Users.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ export const Users: CollectionConfig = {
3535
useAsTitle: 'displayName',
3636
defaultColumns: [
3737
'displayName',
38-
// "firstName",
39-
// "lastName",
4038
'pronouns',
4139
'email',
4240
'updatedAt',
@@ -175,6 +173,45 @@ export const Users: CollectionConfig = {
175173
{ name: 'emergencyContactEmailAddress', type: 'email', label: 'Email Address' },
176174
],
177175
},
176+
{
177+
name: 'linkedinSub',
178+
type: 'text',
179+
admin: {
180+
readOnly: true,
181+
// condition: (data, siblingData, { user }) => {
182+
// return false
183+
},
184+
},
185+
{
186+
name: 'linkedinEmailVerified',
187+
type: 'text',
188+
admin: {
189+
readOnly: true,
190+
},
191+
},
192+
{
193+
name: 'linkedinLocale',
194+
type: 'text',
195+
admin: {
196+
readOnly: true,
197+
},
198+
},
199+
{
200+
name: 'googleSub',
201+
type: 'text',
202+
admin: {
203+
readOnly: true,
204+
// condition: (data, siblingData, { user }) => {
205+
// return false
206+
},
207+
},
208+
{
209+
name: 'googleEmailVerified',
210+
type: 'text',
211+
admin: {
212+
readOnly: true,
213+
},
214+
},
178215
],
179216
timestamps: true,
180217
}

0 commit comments

Comments
 (0)