From 2062a579d35657db7486622c07015f57dc9d9c6c Mon Sep 17 00:00:00 2001 From: Peter Phanouvong Date: Fri, 5 Jul 2024 11:29:38 +1000 Subject: [PATCH] feat: KINDE_SCOPE + getUser upgrades --- src/config/index.js | 5 +++-- src/routerClients/AppRouterClient.js | 5 +++-- src/routerClients/PagesRouterClient.js | 2 +- src/session/getUser.js | 12 ++++++++++++ types.d.ts | 2 ++ 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/config/index.js b/src/config/index.js index a152dbd..649e041 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -54,6 +54,7 @@ const KINDE_AUDIENCE = process.env.KINDE_AUDIENCE; const KINDE_COOKIE_DOMAIN = removeTrailingSlash( process.env.KINDE_COOKIE_DOMAIN ); +const KINDE_SCOPE = process.env.KINDE_SCOPE || 'openid profile email offline'; const isDebugMode = process.env.KINDE_DEBUG_MODE === 'true'; @@ -71,7 +72,6 @@ export const config = { audience: KINDE_AUDIENCE, cookieDomain: KINDE_COOKIE_DOMAIN, responseType: 'code', - scope: 'openid profile email offline', codeChallengeMethod: 'S256', redirectRoutes: { callback: `${KINDE_AUTH_API_PATH}/kinde_callback` @@ -91,7 +91,8 @@ export const config = { logoutRedirectURL: KINDE_POST_LOGOUT_REDIRECT_URL || '', redirectURL: `${KINDE_SITE_URL}/api/auth/kinde_callback`, frameworkVersion: version, - framework: 'Next.js' + framework: 'Next.js', + scope: KINDE_SCOPE }, grantType: GrantType.AUTHORIZATION_CODE }; diff --git a/src/routerClients/AppRouterClient.js b/src/routerClients/AppRouterClient.js index b59f083..58a31d3 100644 --- a/src/routerClients/AppRouterClient.js +++ b/src/routerClients/AppRouterClient.js @@ -11,7 +11,7 @@ export default class AppRouterClient extends RouterClient { * * @param {NextRequest} req * @param {*} res - * @param {{onError?: () => void; config: {audience?: string | string[], clientId?: string, clientSecret?: string, issuerURL?: string, siteUrl?: string, postLoginRedirectUrl?: string, postLogoutRedirectUrl?: string}}} options + * @param {{onError?: () => void; config: {audience?: string | string[], clientId?: string, clientSecret?: string, issuerURL?: string, siteUrl?: string, postLoginRedirectUrl?: string, postLogoutRedirectUrl?: string, scope?: string}}} options */ constructor(req, res, options) { super(); @@ -28,7 +28,8 @@ export default class AppRouterClient extends RouterClient { redirectURL: options?.config?.siteUrl ? `${options?.config?.siteUrl}/api/auth/kinde_callback` : config.clientOptions.redirectURL, - siteUrl: config.redirectURL || options.config.siteUrl + siteUrl: config.redirectURL || options.config.siteUrl, + scope: options?.config?.scope || config.clientOptions.scope }; this.kindeClient = createKindeServerClient( config.grantType, diff --git a/src/routerClients/PagesRouterClient.js b/src/routerClients/PagesRouterClient.js index f519eeb..c816efd 100644 --- a/src/routerClients/PagesRouterClient.js +++ b/src/routerClients/PagesRouterClient.js @@ -9,7 +9,7 @@ export default class PagesRouterClient extends RouterClient { * * @param {import('next').NextApiRequest} req * @param {import('next').NextApiResponse} res - * @param {{onError?: () => void; config: {audience?: string | string[], clientId?: string, clientSecret?: string, issuerURL?: string, siteUrl?: string, postLoginRedirectUrl?: string, postLogoutRedirectUrl?: string}}} options + * @param {{onError?: () => void; config: {audience?: string | string[], clientId?: string, clientSecret?: string, issuerURL?: string, siteUrl?: string, postLoginRedirectUrl?: string, postLogoutRedirectUrl?: string, scope?: string}}} options */ constructor(req, res, options) { super(); diff --git a/src/session/getUser.js b/src/session/getUser.js index 3573fd5..7fb6498 100644 --- a/src/session/getUser.js +++ b/src/session/getUser.js @@ -20,8 +20,20 @@ export const getUserFactory = (req, res) => async () => { sessionManager(req, res), 'user_properties' ); + const phone_number = await kindeClient.getClaimValue( + sessionManager(req, res), + 'phone_number', + 'id_token' + ); + const username = await kindeClient.getClaimValue( + sessionManager(req, res), + 'preferred_username', + 'id_token' + ); return { ...user, + phone_number, + username, properties: { city: userProperties?.kp_usr_city?.v, industry: userProperties?.kp_usr_industry?.v, diff --git a/types.d.ts b/types.d.ts index ca3fa3b..e49ddb5 100644 --- a/types.d.ts +++ b/types.d.ts @@ -48,6 +48,8 @@ export type KindeUser = { given_name: string | null; family_name: string | null; picture: string | null; + username?: string | null; + phone_number?: string | null; properties?: { usr_city?: string; usr_industry?: string;