Skip to content

Commit

Permalink
Feat: Custom userProfile for Oauth2 to support login with Keycloak; I…
Browse files Browse the repository at this point in the history
…nclude phone to customer when logging in store (#155)
  • Loading branch information
sl1mpshady authored Mar 28, 2024
1 parent 262932f commit 859adb7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
32 changes: 31 additions & 1 deletion docs/pages/authentication/oauth2.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,37 @@ newly added plugins. To do so here are the steps
// verifyCallback: (container, req, accessToken, refreshToken, profile, strict) => {
// // implement your custom verify callback here if you need it
// }
}
},
// parseProfile: (json) => {
// const profile = {
// provider: "keycloak",
// id: json.sub,
// username: json.preferred_username,
// displayName: json.name,
// email: json.email,
// name: {
// familyName: json.family_name,
// givenName: json.given_name,
// },
// emails: json.email
// ? [
// {
// value: json.email,
// },
// ]
// : [],
// _json: json,
// phoneNumbers: json.phone_number
// ? [
// {
// value: json.phone_number ?? "",
// },
// ]
// : [],
// };

// return profile;
// },
}
]
}
Expand Down
17 changes: 17 additions & 0 deletions packages/medusa-plugin-auth/src/auth-strategies/oauth2/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ export function getOAuth2AdminStrategy(id: string): StrategyFactory<OAuth2AuthOp
strategyName,
});
}

userProfile(accessToken, done: (err: any, profile?: any) => void) {
if (this.strategyOptions.parseProfile !== undefined) {
let json;

try {
json = JSON.parse(Buffer.from(accessToken.split('.')[1], 'base64').toString());
} catch (ex) {
return done(new Error('Failed to parse access token'));
}

const profile = this.strategyOptions.parseProfile(json);
done(null, profile);
} else {
super.userProfile(accessToken, done);
}
}
};
}

Expand Down
17 changes: 17 additions & 0 deletions packages/medusa-plugin-auth/src/auth-strategies/oauth2/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ export function getOAuth2StoreStrategy(id: string): StrategyFactory<OAuth2AuthOp
strategyName,
});
}

userProfile(accessToken, done: (err: any, profile?: any) => void) {
if (this.strategyOptions.parseProfile !== undefined) {
let json;

try {
json = JSON.parse(Buffer.from(accessToken.split('.')[1], 'base64').toString());
} catch (ex) {
return done(new Error('Failed to parse access token'));
}

const profile = this.strategyOptions.parseProfile(json);
done(null, profile);
} else {
super.userProfile(accessToken, done);
}
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ export type OAuth2AuthOptions = {
expiresIn?: number;
};
scope?: string[];
parseProfile?: (profile: any) => any;
};
2 changes: 2 additions & 0 deletions packages/medusa-plugin-auth/src/core/validate-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export async function validateStoreCallback<
email_verified?: boolean;
};
emails?: { value: string }[];
phoneNumbers?: { value: string }[];
} = {
emails?: { value: string }[];
}
Expand Down Expand Up @@ -173,6 +174,7 @@ export async function validateStoreCallback<
last_name: profile.name?.familyName ?? '',
has_account: true,
password: generatePassword(),
phone: profile.phoneNumbers?.[0]?.value ?? '',
});

return { id: customer.id };
Expand Down

0 comments on commit 859adb7

Please sign in to comment.