-
Notifications
You must be signed in to change notification settings - Fork 0
AUTH-10 Create session actions #6
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| "use server"; | ||
| import { prisma } from "./prisma"; | ||
| import crypto from "crypto"; | ||
|
|
||
| function generateSessionToken() { | ||
| return crypto.randomBytes(32).toString("hex"); // 64-char token | ||
| } | ||
|
|
||
| function hashToken(token: string) { | ||
| return crypto.createHash("sha256").update(token).digest("hex"); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new session with a userId and projectId | ||
| * @param userId The userId of the user for the session | ||
| * @param projectId The projectId of the project for the session | ||
| * @returns The created session | ||
| */ | ||
| export async function createSession(userId: string, projectId: string) { | ||
| const rawToken = generateSessionToken(); | ||
| const hashedToken = hashToken(rawToken); | ||
|
|
||
| const expiresAt = new Date(); | ||
| expiresAt.setDate(expiresAt.getDate() + 1); // 1 day | ||
| const session = prisma.session.create({ | ||
| data: { | ||
| userId, | ||
| projectId, | ||
| token: hashedToken, | ||
| expiresAt, | ||
| }, | ||
| }); | ||
| return { | ||
| ...session, | ||
| token: rawToken, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Gets a session based on a session id | ||
| * @param id The session id | ||
| * @returns The session | ||
| */ | ||
| export async function getSession(id: string) { | ||
| prisma.session.findUnique({ where: { id, expiresAt: { gt: new Date() } } }); | ||
| } | ||
|
|
||
| /** | ||
| * Gets all sessions by filter | ||
| * @param filters userId and projectId | ||
| * @returns All sessions associated with the filters | ||
| */ | ||
| export async function getSessions(filters: { | ||
| userId: string; | ||
| projectId: string; | ||
| }) { | ||
| const sessions = await prisma.session.findMany({ | ||
| where: { | ||
| ...(filters?.userId && { userId: filters.userId }), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ...(filters?.projectId && { projectId: filters.projectId }), | ||
| expiresAt: { | ||
| gt: new Date(), // only active sessions | ||
| }, | ||
| }, | ||
| orderBy: { | ||
| createdAt: "desc", | ||
| }, | ||
| }); | ||
|
|
||
| if (!sessions) return null; | ||
|
|
||
| return sessions; | ||
| } | ||
|
|
||
| /** | ||
| * Updates a session to extend the expireAt | ||
| * @param id The session id | ||
| * @param data When the new expireAt should be | ||
| * @returns The updated session | ||
| */ | ||
| export async function updateSession(id: string, data: { expiresAt: Date }) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const session = await prisma.session.findUnique({ where: { id } }); | ||
| if (!session || session.expiresAt < new Date()) return null; | ||
| return prisma.session.update({ | ||
| where: { id }, | ||
| data: { expiresAt: data.expiresAt }, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a session | ||
| * @param id The session id | ||
| * @returns If the delete was successful | ||
| */ | ||
| export async function deleteSession(id: string) { | ||
| return prisma.session.delete({ where: { id } }); | ||
| } | ||
|
|
||
| /** | ||
| * Validates a session based on a token and project | ||
| * @param token The token associated with the session | ||
| * @param projectId The projectId associated with a project | ||
| * @returns a session | ||
| */ | ||
| export async function validateSession(token: string, projectId: string) { | ||
| const hashedToken = hashToken(token); | ||
| return prisma.session.findFirst({ | ||
| where: { | ||
| token: hashedToken, | ||
| projectId, | ||
| expiresAt: { | ||
| gt: new Date(), | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
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.
prisma.session.create(...)returns a lazyPrismaPromise, and its own enumerable shape includes athen— so{ ...session, token: rawToken }below produces a thenable object. When the caller doesawait 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), andtoken: rawTokengets 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 explicitawaithere and constructing the return object from the resolved row, independent of anything else in this review.