Skip to content

Commit

Permalink
session functions
Browse files Browse the repository at this point in the history
  • Loading branch information
peterphanouvong committed Sep 22, 2023
1 parent d768cbf commit 186b0ff
Show file tree
Hide file tree
Showing 40 changed files with 165 additions and 331 deletions.
8 changes: 1 addition & 7 deletions src/server/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
// export {handleAuth} from '../handlers/appRouter/index';
import * as serverSession from '../session/appRouter/index';

export const getKindeServerSession = () => {
return serverSession;
};

export {getKindeServerSession} from '../session/index';
export {authMiddleware} from '../authMiddleware/authMiddleware';

export {RegisterLink} from '../components/RegisterLink';
Expand Down
18 changes: 0 additions & 18 deletions src/session/appRouter/getAccessToken.js

This file was deleted.

10 changes: 0 additions & 10 deletions src/session/appRouter/getBooleanFlag.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/session/appRouter/getClaim.js

This file was deleted.

32 changes: 0 additions & 32 deletions src/session/appRouter/getFlag.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/session/appRouter/getIdToken.js

This file was deleted.

10 changes: 0 additions & 10 deletions src/session/appRouter/getIntegerFlag.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/session/appRouter/getOrganization.js

This file was deleted.

10 changes: 0 additions & 10 deletions src/session/appRouter/getPermission.js

This file was deleted.

10 changes: 0 additions & 10 deletions src/session/appRouter/getPermissions.js

This file was deleted.

10 changes: 0 additions & 10 deletions src/session/appRouter/getStringFlag.js

This file was deleted.

16 changes: 0 additions & 16 deletions src/session/appRouter/getUser.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/session/appRouter/getUserOrganizations.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/session/appRouter/index.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/session/appRouter/isAuthenticated.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/session/getAccessToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {sessionManager} from './sessionManager';

export const getAccessTokenFactory = (req, res) => async () => {
return await sessionManager(req, res).getSessionItem('access_token_payload');
};
12 changes: 12 additions & 0 deletions src/session/getBooleanFlag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {getFlagFactory} from './getFlag';

export const getBooleanFlagFactory =
(req, res) => async (code, defaultValue) => {
try {
const flag = await getFlagFactory(req, res)(code, defaultValue, 'b');
return flag.value;
} catch (err) {
console.error(err);
return null;
}
};
21 changes: 21 additions & 0 deletions src/session/getFlag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

export const getFlagFactory =
(req, res) => async (code, defaultValue, flagType) => {
try {
const flag = await kindeClient.getFlag(
sessionManager(req, res),
code,
defaultValue,
flagType
);

return flag;
} catch (error) {
if (error.message.includes('no default value has been provided')) {
throw error;
}
return {value: defaultValue};
}
};
12 changes: 12 additions & 0 deletions src/session/getIntegerFlag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {getFlagFactory} from './getFlag';

export const getIntegerFlagFactory =
(req, res) => async (code, defaultValue) => {
try {
const flag = await getFlagFactory(req, res)(code, defaultValue, 'i');
return flag.value;
} catch (err) {
console.error(err);
return null;
}
};
13 changes: 13 additions & 0 deletions src/session/getOrganization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

export const getOrganizationFactory = (req, res) => async () => {
try {
const organization = await kindeClient.getOrganization(
sessionManager(req, res)
);
return organization;
} catch (error) {
return null;
}
};
14 changes: 14 additions & 0 deletions src/session/getPermission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

export const getPermissionFactory = (req, res) => async (name) => {
try {
const permission = await kindeClient.getPermission(
sessionManager(req, res),
name
);
return permission;
} catch (error) {
return null;
}
};
13 changes: 13 additions & 0 deletions src/session/getPermissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

export const getPermissionsFactory = (req, res) => async () => {
try {
const permissions = await kindeClient.getPermissions(
sessionManager(req, res)
);
return permissions;
} catch (error) {
return null;
}
};
12 changes: 12 additions & 0 deletions src/session/getStringFlag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {getFlagFactory} from './getFlag';

export const getStringFlagFactory =
(req, res) => async (code, defaultValue) => {
try {
const flag = await getFlagFactory(req, res)(code, defaultValue, 's');
return flag.value;
} catch (err) {
console.error(err);
return null;
}
};
11 changes: 11 additions & 0 deletions src/session/getUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

export const getUserFactory = (req, res) => async () => {
try {
const user = await kindeClient.getUser(sessionManager(req, res));
return user;
} catch (error) {
return null;
}
};
13 changes: 13 additions & 0 deletions src/session/getUserOrganizations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';

export const getUserOrganizationsFactory = (req, res) => async () => {
try {
const userOrgs = await kindeClient.getUserOrganizations(
sessionManager(req, res)
);
return userOrgs;
} catch (error) {
return null;
}
};
25 changes: 25 additions & 0 deletions src/session/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {getAccessTokenFactory} from './getAccessToken';
import {getBooleanFlagFactory} from './getBooleanFlag';
import {getFlagFactory} from './getFlag';
import {getIntegerFlagFactory} from './getIntegerFlag';
import {getOrganizationFactory} from './getOrganization';
import {getPermissionFactory} from './getPermission';
import {getPermissionsFactory} from './getPermissions';
import {getStringFlagFactory} from './getStringFlag';
import {getUserFactory} from './getUser';
import {getUserOrganizationsFactory} from './getUserOrganizations';
import {isAuthenticatedFactory} from './isAuthenticated';

export const getKindeServerSession = (req, res) => ({
getAccessToken: getAccessTokenFactory(req, res),
getBooleanFlag: getBooleanFlagFactory(req, res),
getFlag: getFlagFactory(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
@@ -0,0 +1,6 @@
import {getUserFactory} from './getUser';

export const isAuthenticatedFactory = (req, res) => async () => {
const user = await getUserFactory(req, res)();
return Boolean(user);
};
7 changes: 7 additions & 0 deletions src/session/kindeServerClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {createKindeServerClient} from '@kinde-oss/kinde-typescript-sdk';
import {config} from '../config/index';

export const kindeClient = createKindeServerClient(
config.grantType,
config.clientOptions
);
15 changes: 0 additions & 15 deletions src/session/pagesRouter/getAccessToken.js

This file was deleted.

Loading

0 comments on commit 186b0ff

Please sign in to comment.