forked from Northgate-Systems/RemitX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
87 lines (74 loc) · 2.41 KB
/
Copy pathauth.ts
File metadata and controls
87 lines (74 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";
import { cookies } from "next/headers";
import { db } from "./db";
import type { User } from "@/generated/prisma/client";
const JWT_SECRET = process.env.JWT_SECRET || "dev-jwt-secret-change-in-production-min-32-chars-long";
const SESSION_COOKIE = "remitx_session";
const SALT_ROUNDS = 12;
export type SafeUser = Omit<User, "passwordHash">;
/** Hash a plaintext password */
export async function hashPassword(password: string): Promise<string> {
return bcrypt.hash(password, SALT_ROUNDS);
}
/** Compare a plaintext password against a hash */
export async function verifyPassword(password: string, hash: string): Promise<boolean> {
return bcrypt.compare(password, hash);
}
/** Generate a JWT for a user */
export function signToken(user: SafeUser): string {
return jwt.sign(
{ sub: user.id, email: user.email },
JWT_SECRET,
{ expiresIn: "7d" }
);
}
/** Verify and decode a JWT */
export function verifyToken(token: string): { sub: string; email: string } | null {
try {
return jwt.verify(token, JWT_SECRET) as { sub: string; email: string };
} catch {
return null;
}
}
/** Set the session cookie */
export async function setSessionCookie(token: string): Promise<void> {
const cookieStore = await cookies();
cookieStore.set(SESSION_COOKIE, token, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
path: "/",
maxAge: 60 * 60 * 24 * 7, // 7 days
});
}
/** Clear the session cookie */
export async function clearSessionCookie(): Promise<void> {
const cookieStore = await cookies();
cookieStore.set(SESSION_COOKIE, "", {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
path: "/",
maxAge: 0,
});
}
/** Get the current user from the session cookie */
export async function getCurrentUser(): Promise<SafeUser | null> {
const cookieStore = await cookies();
const token = cookieStore.get(SESSION_COOKIE)?.value;
if (!token) return null;
const payload = verifyToken(token);
if (!payload) return null;
const user = await db.user.findUnique({
where: { id: payload.sub },
});
if (!user) return null;
const { passwordHash: _, ...safeUser } = user;
return safeUser;
}
/** Strip password hash from user object */
export function toSafeUser(user: User): SafeUser {
const { passwordHash: _, ...safeUser } = user;
return safeUser;
}