-
Notifications
You must be signed in to change notification settings - Fork 7
Partially replace deprecated Kinde TS SDK with js-utils ExpressStore and enhance session management #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dtoxvanilla1991
wants to merge
2
commits into
kinde-oss:main
Choose a base branch
from
dtoxvanilla1991:feat/js-utils-express-manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Partially replace deprecated Kinde TS SDK with js-utils ExpressStore and enhance session management #85
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,33 @@ | ||
| import type { Express } from "express"; | ||
| import { | ||
| type SetupConfig, | ||
| getInternalClient, | ||
| setupInternalClient, | ||
| } from "./setup/index.js"; | ||
| import { setupAuthRouter } from "./auth/index.js"; | ||
| import type { Express } from "express"; | ||
| import { jwtVerify } from "./helpers/kindeMiddlewareHelpers.js"; | ||
|
|
||
| import { | ||
| managementApi, | ||
| GrantType, | ||
| Configuration, | ||
| } from "@kinde-oss/kinde-typescript-sdk"; | ||
|
|
||
| import { setupKindeSession } from "./setup/sessionManager.js"; | ||
| import type { GrantType } from "@kinde-oss/kinde-typescript-sdk"; | ||
| export * from "./helpers/index.js"; | ||
| export { managementApi, GrantType, Configuration, jwtVerify }; | ||
| export { jwtVerify }; | ||
|
|
||
| /** | ||
| * Encapsulates Kinde setup by completing creating internal TypeScript SDK | ||
| * client, setting up its session manager interface and attaching auth router | ||
| * to provided express instance. | ||
| * Encapsulates Kinde setup for an Express application by setting up sessions, | ||
| * creating the client, and attaching authentication routes. | ||
| * | ||
| * @param {Express} app | ||
| * @param {SetupConfig} config | ||
| * @param {SetupConfig} config The Kinde configuration object. | ||
| * @param {Express} app The Express application instance. | ||
| * @returns The initialized Kinde client. | ||
| */ | ||
| export const setupKinde = <G extends GrantType>( | ||
| config: SetupConfig<G>, | ||
| app: Express, | ||
| ) => { | ||
| setupInternalClient(app, config); | ||
| // setting up expressSession layer first for Kinde | ||
| setupKindeSession(app); | ||
| // initializing the Kinde SDK client | ||
| setupInternalClient(config); | ||
| // setting up the authentication routes | ||
| setupAuthRouter(app, "/"); | ||
| return getInternalClient(); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,10 @@ | ||
| import { setupKindeSession } from "./sessionManager.js"; | ||
| import { setupInternalClient as setupKindeClient } from "./kindeClient.js"; | ||
| import type { GrantType } from "@kinde-oss/kinde-typescript-sdk"; | ||
| import type { SetupConfig } from "./kindeSetupTypes.js"; | ||
| import type { Express } from "express"; | ||
| // this file only re-exports the necessary functions and types from the other files | ||
| // in the setup directory | ||
| export { | ||
| getInternalClient, | ||
| getInitialConfig, | ||
| setupInternalClient, | ||
| } from "./kindeClient.js"; | ||
|
|
||
| export { getInternalClient, getInitialConfig } from "./kindeClient.js"; | ||
| export { getSessionManager, setupKindeSession } from "./sessionManager.js"; | ||
| export * from "./kindeSetupTypes.js"; | ||
|
|
||
| /** | ||
| * Encapsulates Kinde setup of creatint creating internal TypeScript SDK | ||
| * client, setting up its session manager interface and attaching session | ||
| * manager to provided express instance. | ||
| * | ||
| * @param {Express} app | ||
| * @param {SetupConfig} config | ||
| */ | ||
| export const setupInternalClient = <G extends GrantType>( | ||
| app: Express, | ||
| config: SetupConfig<G>, | ||
| ): void => { | ||
| setupKindeSession(app); | ||
| setupKindeClient(config); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { describe, it, expect, beforeEach } from "vitest"; | ||
| import express, { type Express } from "express"; | ||
| import request from "supertest"; | ||
| import session from "express-session"; | ||
| import { setupKindeSession } from "./sessionManager.js"; | ||
|
|
||
| describe("sessionManager", () => { | ||
| let app: Express; | ||
|
|
||
| beforeEach(() => { | ||
| app = express(); | ||
| app.use(express.json()); | ||
| }); | ||
|
|
||
| describe("setupKindeSession()", () => { | ||
| it("should add session middleware if it has not been added", () => { | ||
| setupKindeSession(app); | ||
| const sessionMiddleware = app._router.stack.find( | ||
| (layer) => layer.name === "session", | ||
| ); | ||
| expect(sessionMiddleware).toBeDefined(); | ||
| }); | ||
|
|
||
| it("should not add session middleware if it has already been added", () => { | ||
| app.use( | ||
| session({ | ||
| secret: | ||
| process.env.TEST_SESSION_SECRET || "test-secret-" + Date.now(), | ||
| resave: false, | ||
| saveUninitialized: true, | ||
| }), | ||
| ); | ||
| const sessionMiddlewareCount = app._router.stack.filter( | ||
| (layer) => layer.name === "session", | ||
| ).length; | ||
| expect(sessionMiddlewareCount).toBe(1); | ||
| setupKindeSession(app); | ||
| const sessionMiddlewareCountAfter = app._router.stack.filter( | ||
| (layer) => layer.name === "session", | ||
| ).length; | ||
| expect(sessionMiddlewareCountAfter).toBe(1); | ||
| }); | ||
|
|
||
| it("should add getSessionManager middleware", () => { | ||
| const initialStackLength = app._router.stack.length; | ||
| setupKindeSession(app); | ||
| const finalStackLength = app._router.stack.length; | ||
| expect(finalStackLength).toBeGreaterThan(initialStackLength); | ||
| }); | ||
| }); | ||
|
|
||
| describe("ExpressSessionManager functionality", () => { | ||
| beforeEach(() => { | ||
| setupKindeSession(app); | ||
| app.get("/session-functions", (req, res) => { | ||
| res.json({ | ||
| setSessionItem: typeof req.setSessionItem === "function", | ||
| getSessionItem: typeof req.getSessionItem === "function", | ||
| removeSessionItem: typeof req.removeSessionItem === "function", | ||
| destroySession: typeof req.destroySession === "function", | ||
| }); | ||
| }); | ||
|
|
||
| app.post("/set-item", async (req, res) => { | ||
| const { key, value } = req.body; | ||
| await req.setSessionItem(key, value); | ||
| res.sendStatus(200); | ||
| }); | ||
|
|
||
| app.get("/get-item", async (req, res) => { | ||
| const { key } = req.query; | ||
| const value = await req.getSessionItem(key as string); | ||
| res.json({ value }); | ||
| }); | ||
|
|
||
| app.post("/remove-item", async (req, res) => { | ||
| const { key } = req.body; | ||
| await req.removeSessionItem(key); | ||
| res.sendStatus(200); | ||
| }); | ||
|
|
||
| app.post("/destroy", async (req, res) => { | ||
| try { | ||
| await req.destroySession(); | ||
| res.sendStatus(200); | ||
| } catch { | ||
| res.status(500).send("Error destroying session"); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it("adds ExpressSessionManager methods to the request object", async () => { | ||
| const res = await request(app).get("/session-functions"); | ||
| expect(res.status).toBe(200); | ||
| expect(res.body).toEqual({ | ||
| setSessionItem: true, | ||
| getSessionItem: true, | ||
| removeSessionItem: true, | ||
| destroySession: true, | ||
| }); | ||
| }); | ||
|
|
||
| it("sets and gets a session item via ExpressSessionManager", async () => { | ||
| const agent = request.agent(app); | ||
| await agent | ||
| .post("/set-item") | ||
| .send({ key: "testKey", value: "testValue" }); | ||
| const res = await agent.get("/get-item").query({ key: "testKey" }); | ||
| expect(res.body.value).toBe("testValue"); | ||
| }); | ||
|
|
||
| it("removes a session item via ExpressSessionManager", async () => { | ||
| const agent = request.agent(app); | ||
| await agent | ||
| .post("/set-item") | ||
| .send({ key: "testKey", value: "testValue" }); | ||
| await agent.post("/remove-item").send({ key: "testKey" }); | ||
| const res = await agent.get("/get-item").query({ key: "testKey" }); | ||
| expect(res.body.value).toBe(null); | ||
| }); | ||
|
|
||
| it("destroys the session via ExpressSessionManager", async () => { | ||
| const agent = request.agent(app); | ||
| await agent | ||
| .post("/set-item") | ||
| .send({ key: "testKey", value: "testValue" }); | ||
| await agent.post("/destroy"); | ||
| const res = await agent.get("/get-item").query({ key: "testKey" }); | ||
| expect(res.body.value).toBe(null); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this be called ExpressStore in js-utils?