|
| 1 | +"use server"; |
| 2 | +import { prisma } from "./prisma"; |
| 3 | +import crypto from "crypto"; |
| 4 | + |
| 5 | +function generateSessionToken() { |
| 6 | + return crypto.randomBytes(32).toString("hex"); // 64-char token |
| 7 | +} |
| 8 | + |
| 9 | +function hashToken(token: string) { |
| 10 | + return crypto.createHash("sha256").update(token).digest("hex"); |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Creates a new session with a userId and projectId |
| 15 | + * @param userId The userId of the user for the session |
| 16 | + * @param projectId The projectId of the project for the session |
| 17 | + * @returns The created session |
| 18 | + */ |
| 19 | +export async function createSession(userId: string, projectId: string) { |
| 20 | + const rawToken = generateSessionToken(); |
| 21 | + const hashedToken = hashToken(rawToken); |
| 22 | + |
| 23 | + const expiresAt = new Date(); |
| 24 | + expiresAt.setDate(expiresAt.getDate() + 1); // 1 day |
| 25 | + const session = prisma.session.create({ |
| 26 | + data: { |
| 27 | + userId, |
| 28 | + projectId, |
| 29 | + token: hashedToken, |
| 30 | + expiresAt, |
| 31 | + }, |
| 32 | + }); |
| 33 | + return { |
| 34 | + ...session, |
| 35 | + token: rawToken, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Gets a session based on a session id |
| 41 | + * @param id The session id |
| 42 | + * @returns The session |
| 43 | + */ |
| 44 | +export async function getSession(id: string) { |
| 45 | + prisma.session.findUnique({ where: { id, expiresAt: { gt: new Date() } } }); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Gets all sessions by filter |
| 50 | + * @param filters userId and projectId |
| 51 | + * @returns All sessions associated with the filters |
| 52 | + */ |
| 53 | +export async function getSessions(filters: { |
| 54 | + userId: string; |
| 55 | + projectId: string; |
| 56 | +}) { |
| 57 | + const sessions = await prisma.session.findMany({ |
| 58 | + where: { |
| 59 | + ...(filters?.userId && { userId: filters.userId }), |
| 60 | + ...(filters?.projectId && { projectId: filters.projectId }), |
| 61 | + expiresAt: { |
| 62 | + gt: new Date(), // only active sessions |
| 63 | + }, |
| 64 | + }, |
| 65 | + orderBy: { |
| 66 | + createdAt: "desc", |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + if (!sessions) return null; |
| 71 | + |
| 72 | + return sessions; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Updates a session to extend the expireAt |
| 77 | + * @param id The session id |
| 78 | + * @param data When the new expireAt should be |
| 79 | + * @returns The updated session |
| 80 | + */ |
| 81 | +export async function updateSession(id: string, data: { expiresAt: Date }) { |
| 82 | + const session = await prisma.session.findUnique({ where: { id } }); |
| 83 | + if (!session || session.expiresAt < new Date()) return null; |
| 84 | + return prisma.session.update({ |
| 85 | + where: { id }, |
| 86 | + data: { expiresAt: data.expiresAt }, |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Deletes a session |
| 92 | + * @param id The session id |
| 93 | + * @returns If the delete was successful |
| 94 | + */ |
| 95 | +export async function deleteSession(id: string) { |
| 96 | + return prisma.session.delete({ where: { id } }); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Validates a session based on a token and project |
| 101 | + * @param token The token associated with the session |
| 102 | + * @param projectId The projectId associated with a project |
| 103 | + * @returns a session |
| 104 | + */ |
| 105 | +export async function validateSession(token: string, projectId: string) { |
| 106 | + const hashedToken = hashToken(token); |
| 107 | + return prisma.session.findFirst({ |
| 108 | + where: { |
| 109 | + token: hashedToken, |
| 110 | + projectId, |
| 111 | + expiresAt: { |
| 112 | + gt: new Date(), |
| 113 | + }, |
| 114 | + }, |
| 115 | + }); |
| 116 | +} |
0 commit comments