EllipticElysium
EllipticElysium
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
Yeah, in memory was just a poc so I could figure it out before complicating anything. Thanks for the help getting it sorted :)
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
const app = new Elysia()
.use(cors())
.use(cookie())
.get('/login', async ({ set, setCookie }) => {
const session = SessionManager.createSession();
const loginUrl = await kindeClient.login(session);
setCookie('sessionId', session.sessionId, {
httpOnly: true
})

set.redirect = loginUrl.toString();
})
.get('/login-success', async ({ cookie: { sessionId }, set, path, query }) => {
const session = SessionManager.getSession(sessionId);

if (session) {
const url = new URL(`http://localhost:3000${path}?code=${query.code}&scope=${query.scope}&state=${query.state}`);
await kindeClient.handleRedirectToApp(session, url);

set.redirect = "/";
} else {
throw new Error("Session not found");
}
})
.get("/", () => {
// return SessionManager.sessions;
})
.listen(config.port);
const app = new Elysia()
.use(cors())
.use(cookie())
.get('/login', async ({ set, setCookie }) => {
const session = SessionManager.createSession();
const loginUrl = await kindeClient.login(session);
setCookie('sessionId', session.sessionId, {
httpOnly: true
})

set.redirect = loginUrl.toString();
})
.get('/login-success', async ({ cookie: { sessionId }, set, path, query }) => {
const session = SessionManager.getSession(sessionId);

if (session) {
const url = new URL(`http://localhost:3000${path}?code=${query.code}&scope=${query.scope}&state=${query.state}`);
await kindeClient.handleRedirectToApp(session, url);

set.redirect = "/";
} else {
throw new Error("Session not found");
}
})
.get("/", () => {
// return SessionManager.sessions;
})
.listen(config.port);
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
have done a basic PoC and have got it working, thanks for your help. the key bit i was missing was just not knowing that storing sessionId in cookies was "common practice" here's a rough example for a PoC. I'll write it properly and share that when i get the chance
import { Elysia } from "elysia";
import { cors } from '@elysiajs/cors'
import { cookie } from '@elysiajs/cookie'
import { createKindeServerClient, GrantType } from "@kinde-oss/kinde-typescript-sdk";
import config from "./config";

const kindeClient = createKindeServerClient(GrantType.AUTHORIZATION_CODE, {
authDomain: config.kinde.domain,
clientId: config.kinde.clientId,
clientSecret: config.kinde.clientSecret,
redirectURL: config.kinde.redirectUri,
logoutRedirectURL: config.kinde.logoutUri,
});

class Session {
private store: Record<string, unknown> = {};

constructor(public sessionId: string) {
this.sessionId = sessionId;
}

public async getSessionItem(key: string): Promise<unknown> {
return this.store[key];
}

public async setSessionItem(key: string, value: unknown): Promise<void> {
this.store[key] = value;
}

public async removeSessionItem(key: string): Promise<void> {
delete this.store[key];
}

public async destroySession(): Promise<void> {
this.store = {};
SessionManager.destroySession(this.sessionId);
}
}

class SessionManager {
private static sessions: Record<string, Session> = {};

public static createSession(): Session {
const sessionId = crypto.randomUUID();
const session = new Session(sessionId);
this.sessions[sessionId] = session;
return session;
}

public static getSession(sessionId: string): Session | undefined {
return this.sessions[sessionId];
}

public static destroySession(sessionId: string): void {
delete this.sessions[sessionId];
}
}
import { Elysia } from "elysia";
import { cors } from '@elysiajs/cors'
import { cookie } from '@elysiajs/cookie'
import { createKindeServerClient, GrantType } from "@kinde-oss/kinde-typescript-sdk";
import config from "./config";

const kindeClient = createKindeServerClient(GrantType.AUTHORIZATION_CODE, {
authDomain: config.kinde.domain,
clientId: config.kinde.clientId,
clientSecret: config.kinde.clientSecret,
redirectURL: config.kinde.redirectUri,
logoutRedirectURL: config.kinde.logoutUri,
});

class Session {
private store: Record<string, unknown> = {};

constructor(public sessionId: string) {
this.sessionId = sessionId;
}

public async getSessionItem(key: string): Promise<unknown> {
return this.store[key];
}

public async setSessionItem(key: string, value: unknown): Promise<void> {
this.store[key] = value;
}

public async removeSessionItem(key: string): Promise<void> {
delete this.store[key];
}

public async destroySession(): Promise<void> {
this.store = {};
SessionManager.destroySession(this.sessionId);
}
}

class SessionManager {
private static sessions: Record<string, Session> = {};

public static createSession(): Session {
const sessionId = crypto.randomUUID();
const session = new Session(sessionId);
this.sessions[sessionId] = session;
return session;
}

public static getSession(sessionId: string): Session | undefined {
return this.sessions[sessionId];
}

public static destroySession(sessionId: string): void {
delete this.sessions[sessionId];
}
}
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
though i would still query the naming of sessionManager instead of session but perhaps that's just my personal style/preference 😛
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
awesome, thanks. I'll give that a try :)
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
No description
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
and that seems like a fairly simple thing to do. So i'm assuming there's something i'm missing or just not understanding here...
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
from what I can see, it would be infinitely simpler if the login flow were something like this:
.get('/login', async ({ set }) => {
const session = sessionManager.createSession();
const { sessionId, loginUrl } = await kindeClient.login(session);
sessionManager.saveSession(sessionId, session);
set.redirect = loginUrl.toString();
})
.get('/login-success', async ({ set, path, query }) => {
const session = sessionManager[query.sessionId];
const url = new URL(`http://localhost:3000${path}?code=${query.code}&scope=${query.scope}&state=${query.state}`);
await kindeClient.handleRedirectToApp(session, url);
set.redirect = "/";
})
.get('/login', async ({ set }) => {
const session = sessionManager.createSession();
const { sessionId, loginUrl } = await kindeClient.login(session);
sessionManager.saveSession(sessionId, session);
set.redirect = loginUrl.toString();
})
.get('/login-success', async ({ set, path, query }) => {
const session = sessionManager[query.sessionId];
const url = new URL(`http://localhost:3000${path}?code=${query.code}&scope=${query.scope}&state=${query.state}`);
await kindeClient.handleRedirectToApp(session, url);
set.redirect = "/";
})
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
so, it looks like ac-state-key is what persists between the two requests. I can work with that, but it feels super hacky to intercept the setting of a specific key in order to use that as a key to store my session against. surely there's a better way of doing this?
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
regardless of which sessionManagement i use, i still need to retrieve a specific session to pass into all the Kinde functions. and i've not seen anything in the docs talking about this
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
Looking into just assigning a uuid myself, easy enough to start. i can create a session with that uuid, and pass that session to kindeClient.login(session), that's all good. but when i retrieve the redirect after a successful login, i need to pass the same session into kindeClient.handleRedirectToApp(session, url) or it will fail. so somehow, i need to find the session that the request was initiated with.
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
hi Oli, I've only just had a chance to take another look at this, but i'm still confused about the uuid issue i mentioned. There is nothing i can see in the docs about how to uniquely identify an individual session in order to pass that to kindeClient.login(...) or any other function. building a wrapper is easy if i have a uuid to set and retrieve sessions, but without one 🤷
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
Thanks I'll take a look through that and see if I can get it working. Likely to be the weekend though :)
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
Hey @Oli - Kinde hope you had a good Christmas/New-year. Have you had a chance to look into this any further? :)
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
Can you share any details of what worked that might be relevant to my issue?
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
You managed to figure out how to get it working with the typescript?
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
I'm on the server side using BunJs and ElysiaJs. There won't be any direct sdks, so just assume it's pure typescript without express (though the docs seems to assume it's use)
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
No worries, I'll check back in the new year. Merry Christmas :)
43 replies
KKinde
Created by EllipticElysium on 12/24/2023 in #💻┃support
typescript SDK session manager
hey @Oli - Kinde just wanted to follow up on this, did you manage to get any update? i know is christmas ofc, so no worries at all if not yet. but would you be able to provide an estimated time? :) 🎄
43 replies