Skip to content

AUTH-10 Create session actions - #6

Open
jakewc12 wants to merge 1 commit into
mainfrom
auth-10
Open

AUTH-10 Create session actions#6
jakewc12 wants to merge 1 commit into
mainfrom
auth-10

Conversation

@jakewc12

@jakewc12 jakewc12 commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Created lib/session.ts for managing the Session join table which contains:

createSession
getSession
getSessions
deleteSession
validateSession

@jakewc12
jakewc12 force-pushed the auth-10 branch 2 times, most recently from 6b793df to 5d3e01f Compare April 7, 2026 02:48

@SGAOperations SGAOperations left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few things!

Comment thread src/lib/session.ts.ts Outdated
* @returns The session
*/
export async function getSession(id: string) {
const session = await prisma.session.findUnique({

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make funcs like this inline more?

Comment thread src/lib/session.ts.ts Outdated
},
},
});
} No newline at end of file

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line at end of file pls

Comment thread src/lib/session.ts.ts Outdated
* @returns If the delete was successful
*/
export async function deleteSession(id: string) {
return prisma.session.delete({

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline with this too, anything that is simple enough to go inline

Comment thread src/types/session.type.ts Outdated

export type UpdateSessionData = {
expiresAt?: Date;
}; No newline at end of file

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line end of file pls

Comment thread src/types/session.type.ts Outdated
@@ -0,0 +1,8 @@
export type GetSessionsFilters = {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that these types are complex enough to warrant an externally defined type. Can we just do this inline on the functions that use them?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was having this be outside incase we wanna expand on it in the future, i can just leave it in the file if that's better

Comment thread src/lib/session.ts.ts Outdated
* @param filters userId and projectId
* @returns All sessions associated with the filters
*/
export async function getSessions(filters: GetSessionsFilters) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the use case for this function and should both params be optional? Or should we require at least one? Or should both be required?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Youre right, ill make both required

Comment thread src/lib/session.ts Outdated
return crypto.createHash("sha256").update(token).digest("hex");
}

export type GetSessionsFilters = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should be defined inline with the function

Comment thread src/lib/session.ts Outdated
* @param id The session id
* @returns The session
*/
export const getSession = async (id: string) =>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I would define this with function as regular since defining it as a const makes it different than the rest

Comment thread src/lib/session.ts Outdated
if (!session) return null;

// don't extend expired sessions
if (session.expiresAt < new Date()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to above, can make this part of prisma query

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently prisma doesnt support that, i think this should be fine

Comment thread src/lib/session.ts Outdated
}
return prisma.session.update({
where: { id },
data: {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be inline

Comment thread src/lib/session.ts Outdated
* @param id The session id
* @returns If the delete was successful
*/
export const deleteSession = async (id: string) =>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same not as prev func

@jakewc12 jakewc12 changed the title ATM-10 Create session actions AUTH-10 Create session actions Apr 8, 2026

@pataniaeli pataniaeli left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review written by a Claude agent.

Same framing as the other action-layer PRs open right now: if there's a follow-up ticket for adding caller-identity checks before this is wired into anything, deferring the pure "who's allowed to call this" items is reasonable — separated below so that's explicit.

Must-fix regardless of sequencing (these are functional bugs, not auth gaps):

  • Missing await on prisma.session.create means sessions created via this function currently can't be validated with the token that's returned (see inline comment) — this looks like it hasn't been exercised end-to-end yet.
  • getSessions's empty-string filter bypass (see inline comment) — a plain truthiness bug, not access-control related.
  • getSession (line 45) is missing a return — it always resolves undefined regardless of whether a matching session exists.
  • updateSession accepts an unbounded caller-supplied expiresAt with no cap (see inline comment).
  • Small one: the Session.token column stores a SHA-256 hash (good — not plaintext), but the column name doesn't say so. A rename to tokenHash would help stop a future "fix" from reintroducing plaintext storage when someone's debugging why validation isn't matching.

Pending authz ticket:

  • createSession, getSessions, deleteSession, and validateSession all take userId/projectId as trusted caller input with no identity check. Related but distinct: createSession also doesn't check UserProject membership before issuing a session, which is more of an authorization-scoping question than a pure "is there a caller" one — worth deciding whether that's in scope for the same follow-up.

Worth its own ticket, not introduced by this PR: same RLS gap noted elsewhere — Session sits in the same unprotected public schema.

(Edit: corrected a line reference above and the inline comment anchors below that were off in my first pass — same content, right lines now.)

Comment thread src/lib/session.ts

const expiresAt = new Date();
expiresAt.setDate(expiresAt.getDate() + 1); // 1 day
const session = prisma.session.create({

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prisma.session.create(...) returns a lazy PrismaPromise, and its own enumerable shape includes a then — so { ...session, token: rawToken } below produces a thenable object. When the caller does await createSession(...), that triggers promise assimilation on the returned object rather than treating it as a plain object: the DB write does happen, but the resolved value becomes the created DB record (hashed token), and token: rawToken gets discarded before it ever reaches the caller. Net effect: sessions created this way can't be validated with the token that's actually returned — worth an explicit await here and constructing the return object from the resolved row, independent of anything else in this review.

Comment thread src/lib/session.ts
}) {
const sessions = await prisma.session.findMany({
where: {
...(filters?.userId && { userId: filters.userId }),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filters?.userId && { userId: filters.userId } evaluates to "" (falsy) when userId is an empty string rather than undefined, and spreading {...""} contributes nothing — so an empty-string filter is silently dropped rather than applied. Worth checking for undefined/null explicitly (filters.userId != null) rather than relying on truthiness, since this is a plain logic bug that'll under-filter even for a fully authorized caller who passes an empty string by mistake.

Comment thread src/lib/session.ts
* @param data When the new expireAt should be
* @returns The updated session
*/
export async function updateSession(id: string, data: { expiresAt: Date }) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expiresAt is taken directly from the caller with no upper bound and no check that the session belongs to them — worth clamping to a fixed server-side increment with an absolute cap (e.g. createdAt + N days) rather than trusting the caller's value directly, regardless of what auth ends up gating this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants