Skip to content
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

Migrate old personalTeam to personalOrg in session data #309

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api/src/session-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type SiweFields = Omit<
export interface Auth {
user: schema.User;
personalOrg: schema.Org;
personalTeam?: schema.Org;
}

export interface SessionData {
Expand Down
23 changes: 16 additions & 7 deletions packages/api/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { type Store } from "@tableland/studio-store";
import { TRPCError, initTRPC } from "@trpc/server";
import superjson from "superjson";
import { ZodError, z } from "zod";
import { getIronSession } from "iron-session";
import { type IronSession, getIronSession } from "iron-session";
import { type SessionData, sessionOptions } from "./session-data";

// TODO: the types and interfaces below are from iron-session, but they aren't exported.
Expand Down Expand Up @@ -88,17 +88,26 @@ export const getSession = async function ({
req,
res,
}: GetSessionArgs) {
let session: IronSession<SessionData> | undefined;
if (typeof cookies !== "undefined") {
return await getIronSession<SessionData>(cookies, sessionOptions);
session = await getIronSession<SessionData>(cookies, sessionOptions);
}

if (typeof req !== "undefined" && typeof res !== "undefined") {
return await getIronSession<SessionData>(req, res, sessionOptions);
session = await getIronSession<SessionData>(req, res, sessionOptions);
}
if (!session) {
throw new Error(
"cannot get session from context must supply cookies or req and res",
);
}

throw new Error(
"cannot get session from context must supply cookies or req and res",
);
if (session.auth?.personalTeam) {
session.auth.personalOrg = session.auth.personalTeam;
session.auth.personalTeam = undefined;
await session.save();
}

return session;
};

// get a header from either of the accepted header types
Expand Down
Loading