Skip to content

Commit

Permalink
Merge pull request #82 from kinde-oss/peter/fix/ts-getkindeserversession
Browse files Browse the repository at this point in the history
types: KindeServerSession TS
  • Loading branch information
DaveOrDead authored Nov 3, 2023
2 parents 67dcd9d + 251c6b3 commit ddb5cdb
Show file tree
Hide file tree
Showing 20 changed files with 254 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ export const config = {
profile: '/oauth2/v2/user_profile'
},
clientOptions: {
audience: KINDE_AUDIENCE,
authDomain: KINDE_ISSUER_URL,
clientId: KINDE_CLIENT_ID,
clientSecret: KINDE_CLIENT_SECRET,
logoutRedirectURL: KINDE_POST_LOGOUT_REDIRECT_URL,
audience: KINDE_AUDIENCE || '',
authDomain: KINDE_ISSUER_URL || '',
clientId: KINDE_CLIENT_ID || '',
clientSecret: KINDE_CLIENT_SECRET || '',
logoutRedirectURL: KINDE_POST_LOGOUT_REDIRECT_URL || '',
redirectURL: `${KINDE_SITE_URL}/api/auth/kinde_callback`,
frameworkVersion: version,
framework: 'Next.js'
Expand Down
6 changes: 4 additions & 2 deletions src/handlers/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {config} from '../config/index';

export const callback = async (routerClient) => {
const postLoginRedirectURLFromMemory =
routerClient.sessionManager.getSessionItem('post_login_redirect_url');
await routerClient.sessionManager.getSessionItem('post_login_redirect_url');

if (postLoginRedirectURLFromMemory) {
routerClient.sessionManager.removeSessionItem('post_login_redirect_url');
await routerClient.sessionManager.removeSessionItem(
'post_login_redirect_url'
);
}

const postLoginRedirectURL = postLoginRedirectURLFromMemory
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const login = async (routerClient) => {
);

if (postLoginRedirectURL) {
routerClient.sessionManager.setSessionItem(
await routerClient.sessionManager.setSessionItem(
'post_login_redirect_url',
postLoginRedirectURL
);
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const register = async (routerClient) => {
);

if (postLoginRedirectURL) {
routerClient.sessionManager.setSessionItem(
await routerClient.sessionManager.setSessionItem(
'post_login_redirect_url',
postLoginRedirectURL
);
Expand Down
2 changes: 1 addition & 1 deletion src/server/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {getKindeServerSession} from '../session/index';
export {default as getKindeServerSession} from '../session/index';
export {authMiddleware, withAuth} from '../authMiddleware/authMiddleware';
export {
LoginLink,
Expand Down
11 changes: 9 additions & 2 deletions src/session/getAccessToken.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import {sessionManager} from './sessionManager';

/**
* @callback getAccessToken
* @returns {Promise<import('../../types.js').KindeAccessToken | undefined>}
*/

/**
*
* @param {Request} [req]
* @param {Response} [res]
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
*
* @returns {getAccessToken}
*/
// @ts-ignore
export const getAccessTokenFactory = (req, res) => async () => {
return await sessionManager(req, res).getSessionItem('access_token_payload');
};
13 changes: 13 additions & 0 deletions src/session/getBooleanFlag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import {getFlagFactory} from './getFlag';

/**
* @callback getBooleanFlag
* @param {string} code
* @param {boolean} defaultValue
* @returns {Promise<boolean | null>}
*/

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getBooleanFlag}
*/
export const getBooleanFlagFactory =
(req, res) => async (code, defaultValue) => {
try {
Expand Down
15 changes: 15 additions & 0 deletions src/session/getFlag.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

/**
* @callback getFlag
* @param {string} code
* @param {boolean | number | string} defaultValue
* @param {import('../../types').KindeFlagTypeCode} flagType
* @returns {Promise<import('../../types').KindeFlag | {value: boolean | number | string}>}
*/

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getFlag}
*/
export const getFlagFactory =
(req, res) => async (code, defaultValue, flagType) => {
try {
Expand All @@ -13,6 +27,7 @@ export const getFlagFactory =

return flag;
} catch (error) {
//@ts-ignore
if (error.message.includes('no default value has been provided')) {
throw error;
}
Expand Down
13 changes: 10 additions & 3 deletions src/session/getIdToken.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import {sessionManager} from './sessionManager';

/**
* @callback getIdToken
* @returns {Promise<import('../../types').KindeIdToken>}
*/

/**
*
* @param {Request} [req]
* @param {Response} [res]
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getIdToken}
*/

// @ts-ignore
export const getIdTokenFactory = (req, res) => async () => {
return await sessionManager(req, res).getSessionItem('id_token_payload');
};
13 changes: 13 additions & 0 deletions src/session/getIntegerFlag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import {getFlagFactory} from './getFlag';

/**
* @callback getIntegerFlag
* @param {string} code
* @param {number} defaultValue
* @returns {Promise<number | null>}
*/

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getIntegerFlag}
*/
export const getIntegerFlagFactory =
(req, res) => async (code, defaultValue) => {
try {
Expand Down
11 changes: 11 additions & 0 deletions src/session/getOrganization.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

/**
* @callback getOrganization
* @returns {Promise<import('../../types').KindeOrganization | null>}
*/

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getOrganization}
*/
export const getOrganizationFactory = (req, res) => async () => {
try {
const organization = await kindeClient.getOrganization(
Expand Down
12 changes: 12 additions & 0 deletions src/session/getPermission.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

/**
* @callback getPermission
* @param {string} name
* @returns {Promise<import('../../types').KindePermission | null>}
*/

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getPermission}
*/
export const getPermissionFactory = (req, res) => async (name) => {
try {
const permission = await kindeClient.getPermission(
Expand Down
11 changes: 11 additions & 0 deletions src/session/getPermissions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

/**
* @callback getPermissions
* @returns {Promise<import('../../types').KindePermissions | null>}
*/

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getPermissions}
*/
export const getPermissionsFactory = (req, res) => async () => {
try {
const permissions = await kindeClient.getPermissions(
Expand Down
13 changes: 13 additions & 0 deletions src/session/getStringFlag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import {getFlagFactory} from './getFlag';

/**
* @callback getStringFlag
* @param {string} code
* @param {string} defaultValue
* @returns {Promise<string | null>}
*/

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getStringFlag}
*/
export const getStringFlagFactory =
(req, res) => async (code, defaultValue) => {
try {
Expand Down
11 changes: 11 additions & 0 deletions src/session/getUser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

/**
* @callback getUser
* @returns {Promise<import('../../types').KindeUser | null>}
*/
/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getUser}
*/
export const getUserFactory = (req, res) => async () => {
try {
// @ts-ignore
const user = await kindeClient.getUser(sessionManager(req, res));
return user;
} catch (error) {
Expand Down
11 changes: 11 additions & 0 deletions src/session/getUserOrganizations.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

/**
* @callback getUserOrganizations
* @returns {Promise<import('../../types').KindeOrganizations | null>}
*/

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getUserOrganizations}
*/
export const getUserOrganizationsFactory = (req, res) => async () => {
try {
const userOrgs = await kindeClient.getUserOrganizations(
Expand Down
34 changes: 18 additions & 16 deletions src/session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ import {isAuthenticatedFactory} from './isAuthenticated';

/**
*
* @param {Request} [req]
* @param {Response} [res]
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns
*/
export const getKindeServerSession = (req, res) => ({
getAccessToken: getAccessTokenFactory(req, res),
getBooleanFlag: getBooleanFlagFactory(req, res),
getFlag: getFlagFactory(req, res),
getIdToken: getIdTokenFactory(req, res),
getIntegerFlag: getIntegerFlagFactory(req, res),
getOrganization: getOrganizationFactory(req, res),
getPermission: getPermissionFactory(req, res),
getPermissions: getPermissionsFactory(req, res),
getStringFlag: getStringFlagFactory(req, res),
getUser: getUserFactory(req, res),
getUserOrganizations: getUserOrganizationsFactory(req, res),
isAuthenticated: isAuthenticatedFactory(req, res)
});
export default function (req, res) {
return {
getAccessToken: getAccessTokenFactory(req, res),
getBooleanFlag: getBooleanFlagFactory(req, res),
getFlag: getFlagFactory(req, res),
getIdToken: getIdTokenFactory(req, res),
getIntegerFlag: getIntegerFlagFactory(req, res),
getOrganization: getOrganizationFactory(req, res),
getPermission: getPermissionFactory(req, res),
getPermissions: getPermissionsFactory(req, res),
getStringFlag: getStringFlagFactory(req, res),
getUser: getUserFactory(req, res),
getUserOrganizations: getUserOrganizationsFactory(req, res),
isAuthenticated: isAuthenticatedFactory(req, res)
};
}
6 changes: 6 additions & 0 deletions src/session/isAuthenticated.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {getUserFactory} from './getUser';

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {() => Promise<boolean>}
*/
export const isAuthenticatedFactory = (req, res) => async () => {
const user = await getUserFactory(req, res)();
return Boolean(user);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ddb5cdb

Please sign in to comment.