Skip to content

Commit e144db5

Browse files
committed
added req in getUserInfo, add warning should jwt user is not as expected
1 parent ee5b408 commit e144db5

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

examples/google.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const googleOAuth = OAuth2Plugin({
2323
"https://www.googleapis.com/auth/userinfo.profile",
2424
],
2525
providerAuthorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth",
26-
getUserInfo: async (accessToken: string) => {
26+
getUserInfo: async (accessToken: string, req: PayloadRequest) => {
2727
const response = await fetch(
2828
"https://www.googleapis.com/oauth2/v3/userinfo",
2929
{ headers: { Authorization: `Bearer ${accessToken}` } },

examples/zitadel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const zitadelOAuth = OAuth2Plugin({
2828
"urn:zitadel:iam:user:metadata",
2929
],
3030
providerAuthorizationUrl: process.env.ZITADEL_AUTHORIZATION_URL || "",
31-
getUserInfo: async (accessToken: string) => {
31+
getUserInfo: async (accessToken: string, req: PayloadRequest) => {
3232
const response = await fetch(process.env.ZITADEL_USERINFO_ENDPOINT || "", {
3333
headers: { Authorization: `Bearer ${accessToken}` },
3434
});

src/auth-strategy.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ export const createAuthStrategy = (
5151
let user: User | null = null;
5252

5353
if (pluginOptions.useEmailAsIdentity) {
54-
if (typeof jwtUser.email !== "string") return { user: null };
54+
if (typeof jwtUser.email !== "string") {
55+
payload.logger.warn(
56+
"Using email as identity but no email is found in jwt token",
57+
);
58+
return { user: null };
59+
}
5560
const usersQuery = await payload.find({
5661
collection: userCollection,
5762
where: { email: { equals: jwtUser.email } },
@@ -67,7 +72,12 @@ export const createAuthStrategy = (
6772
user = usersQuery.docs[0] as unknown as User;
6873
}
6974
} else {
70-
if (typeof jwtUser[subFieldName] !== "string") return { user: null };
75+
if (typeof jwtUser[subFieldName] !== "string") {
76+
payload.logger.warn(
77+
`No ${subFieldName} found in jwt token. Make sure the jwt token contains the ${subFieldName} field`,
78+
);
79+
return { user: null };
80+
}
7181
const usersQuery = await payload.find({
7282
collection: userCollection,
7383
where: { [subFieldName]: { equals: jwtUser[subFieldName] } },

src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ export interface PluginTypes {
7070
* This function should return a promise that resolves to the user
7171
* information that will be stored in database.
7272
* @param accessToken Access token obtained from OAuth provider
73+
* @param req PayloadRequest object
7374
*/
74-
getUserInfo: (accessToken: string) => Promise<any> | any;
75+
getUserInfo: (accessToken: string, req: PayloadRequest) => Promise<any> | any;
7576

7677
/**
7778
* Scope for the OAuth provider.
@@ -118,16 +119,21 @@ export interface PluginTypes {
118119
* tokenEndpoint, clientId, clientSecret, redirectUri, code.
119120
*
120121
* Reference: `defaultGetToken` in `src/default-get-token.ts`
122+
* @param code Code obtained from the OAuth provider, used to exchange for access token
123+
* @param req PayloadRequest object
121124
*/
122125
getToken?: (code: string, req: PayloadRequest) => string | Promise<string>;
123126

124127
/**
125128
* Redirect users after successful login.
129+
* @param req PayloadRequest object
126130
*/
127131
successRedirect: (req: PayloadRequest) => string | Promise<string>;
128132

129133
/**
130134
* Redirect users after failed login.
135+
* @param req PayloadRequest object
136+
* @param error Error object
131137
*/
132138
failureRedirect: (
133139
req: PayloadRequest,

0 commit comments

Comments
 (0)