Skip to content

Commit

Permalink
feat: Add openid connect standard claims to user object.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTolmay committed May 25, 2022
1 parent 8bc34a1 commit 7f099e1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
50 changes: 48 additions & 2 deletions packages/api/src/routes/auth/callbacks/createJWTCallback.js
Expand Up @@ -23,9 +23,55 @@ function createJWTCallback({ authConfig, plugins }) {
type: 'jwt',
});

if (jwtCallbackPlugins.length === 0) return undefined;

async function jwtCallback({ token, user, account, profile, isNewUser }) {
if (profile) {
const {
sub,
name,
given_name,
family_name,
middle_name,
nickname,
preferred_username,
profile: profile_claim,
picture,
website,
email,
email_verified,
gender,
birthdate,
zoneinfo,
locale,
phone_number,
phone_number_verified,
address,
updated_at,
} = profile;
token = {
sub,
name,
given_name,
family_name,
middle_name,
nickname,
preferred_username,
profile: profile_claim,
picture,
website,
email,
email_verified,
gender,
birthdate,
zoneinfo,
locale,
phone_number,
phone_number_verified,
address,
updated_at,
...token,
};
}

for (const plugin of jwtCallbackPlugins) {
token = await plugin.fn({
properties: plugin.properties ?? {},
Expand Down
47 changes: 46 additions & 1 deletion packages/api/src/routes/auth/callbacks/createSessionCallback.js
Expand Up @@ -24,8 +24,53 @@ function createSessionCallback({ authConfig, plugins }) {
});

async function sessionCallback({ session, token, user }) {
// console.log({ session, token, user });
if (token) {
session.user.sub = token.sub;
const {
sub,
name,
given_name,
family_name,
middle_name,
nickname,
preferred_username,
profile,
picture,
website,
email,
email_verified,
gender,
birthdate,
zoneinfo,
locale,
phone_number,
phone_number_verified,
address,
updated_at,
} = token;
session.user = {
sub,
name,
given_name,
family_name,
middle_name,
nickname,
preferred_username,
profile,
picture,
website,
email,
email_verified,
gender,
birthdate,
zoneinfo,
locale,
phone_number,
phone_number_verified,
address,
updated_at,
...session.user,
};
}

for (const plugin of sessionCallbackPlugins) {
Expand Down

0 comments on commit 7f099e1

Please sign in to comment.