Skip to content

Commit

Permalink
feat: add email and phonenumber information into oauth access tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Oct 21, 2024
1 parent 9c59148 commit fa78183
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/build/recipe/oauth2provider/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ class Recipe extends recipeModule_1.default {
}
async getDefaultAccessTokenPayload(user, scopes, sessionHandle, userContext) {
let payload = {};
if (scopes.includes("email")) {
payload.email = user === null || user === void 0 ? void 0 : user.emails[0];
payload.email_verified = user.loginMethods.some(
(lm) => lm.hasSameEmailAs(user === null || user === void 0 ? void 0 : user.emails[0]) && lm.verified
);
payload.emails = user.emails;
}
if (scopes.includes("phoneNumber")) {
payload.phoneNumber = user === null || user === void 0 ? void 0 : user.phoneNumbers[0];
payload.phoneNumber_verified = user.loginMethods.some(
(lm) =>
lm.hasSamePhoneNumberAs(user === null || user === void 0 ? void 0 : user.phoneNumbers[0]) &&
lm.verified
);
payload.phoneNumbers = user.phoneNumbers;
}
for (const fn of this.accessTokenBuilders) {
payload = Object.assign(Object.assign({}, payload), await fn(user, scopes, sessionHandle, userContext));
}
Expand Down
14 changes: 14 additions & 0 deletions lib/ts/recipe/oauth2provider/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,20 @@ export default class Recipe extends RecipeModule {
async getDefaultAccessTokenPayload(user: User, scopes: string[], sessionHandle: string, userContext: UserContext) {
let payload: JSONObject = {};

if (scopes.includes("email")) {
payload.email = user?.emails[0];
payload.email_verified = user.loginMethods.some((lm) => lm.hasSameEmailAs(user?.emails[0]) && lm.verified);
payload.emails = user.emails;
}

if (scopes.includes("phoneNumber")) {
payload.phoneNumber = user?.phoneNumbers[0];
payload.phoneNumber_verified = user.loginMethods.some(
(lm) => lm.hasSamePhoneNumberAs(user?.phoneNumbers[0]) && lm.verified
);
payload.phoneNumbers = user.phoneNumbers;
}

for (const fn of this.accessTokenBuilders) {
payload = {
...payload,
Expand Down

0 comments on commit fa78183

Please sign in to comment.