Put src/lib/auth.ts
Browse files- src/lib/auth.ts +81 -0
src/lib/auth.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import "server-only";
|
| 2 |
+
import { cache } from "react";
|
| 3 |
+
import { cookies } from "next/headers";
|
| 4 |
+
import { redirect } from "next/navigation";
|
| 5 |
+
import { randomBytes, scrypt as _scrypt, timingSafeEqual } from "node:crypto";
|
| 6 |
+
import { promisify } from "node:util";
|
| 7 |
+
import { and, eq, gt } from "drizzle-orm";
|
| 8 |
+
import { db } from "@/db";
|
| 9 |
+
import { sessions, users } from "@/db/schema";
|
| 10 |
+
|
| 11 |
+
const scrypt = promisify(_scrypt) as (
|
| 12 |
+
pw: string,
|
| 13 |
+
salt: Buffer,
|
| 14 |
+
len: number,
|
| 15 |
+
) => Promise<Buffer>;
|
| 16 |
+
|
| 17 |
+
const COOKIE = "hf_sid";
|
| 18 |
+
const MAX_AGE = 60 * 60 * 24 * 30;
|
| 19 |
+
|
| 20 |
+
export async function hashPassword(pw: string) {
|
| 21 |
+
const salt = randomBytes(16);
|
| 22 |
+
const key = await scrypt(pw, salt, 64);
|
| 23 |
+
return `${salt.toString("hex")}:${key.toString("hex")}`;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
export async function verifyPassword(pw: string, stored: string) {
|
| 27 |
+
const [saltHex, keyHex] = stored.split(":");
|
| 28 |
+
if (!saltHex || !keyHex) return false;
|
| 29 |
+
const key = await scrypt(pw, Buffer.from(saltHex, "hex"), 64);
|
| 30 |
+
const expected = Buffer.from(keyHex, "hex");
|
| 31 |
+
return key.length === expected.length && timingSafeEqual(key, expected);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
export async function createSession(userId: string) {
|
| 35 |
+
const token = randomBytes(32).toString("hex");
|
| 36 |
+
await db.insert(sessions).values({
|
| 37 |
+
token,
|
| 38 |
+
userId,
|
| 39 |
+
expiresAt: new Date(Date.now() + MAX_AGE * 1000),
|
| 40 |
+
});
|
| 41 |
+
const jar = await cookies();
|
| 42 |
+
jar.set(COOKIE, token, {
|
| 43 |
+
httpOnly: true,
|
| 44 |
+
sameSite: "lax",
|
| 45 |
+
secure: false,
|
| 46 |
+
path: "/",
|
| 47 |
+
maxAge: MAX_AGE,
|
| 48 |
+
});
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
export async function destroySession() {
|
| 52 |
+
const jar = await cookies();
|
| 53 |
+
const token = jar.get(COOKIE)?.value;
|
| 54 |
+
if (token) await db.delete(sessions).where(eq(sessions.token, token));
|
| 55 |
+
jar.delete(COOKIE);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
export const getUser = cache(async () => {
|
| 59 |
+
const jar = await cookies();
|
| 60 |
+
const token = jar.get(COOKIE)?.value;
|
| 61 |
+
if (!token) return null;
|
| 62 |
+
const rows = await db
|
| 63 |
+
.select({
|
| 64 |
+
id: users.id,
|
| 65 |
+
email: users.email,
|
| 66 |
+
name: users.name,
|
| 67 |
+
plan: users.plan,
|
| 68 |
+
onboarded: users.onboarded,
|
| 69 |
+
})
|
| 70 |
+
.from(sessions)
|
| 71 |
+
.innerJoin(users, eq(users.id, sessions.userId))
|
| 72 |
+
.where(and(eq(sessions.token, token), gt(sessions.expiresAt, new Date())))
|
| 73 |
+
.limit(1);
|
| 74 |
+
return rows[0] ?? null;
|
| 75 |
+
});
|
| 76 |
+
|
| 77 |
+
export async function requireUser() {
|
| 78 |
+
const user = await getUser();
|
| 79 |
+
if (!user) redirect("/login");
|
| 80 |
+
return user;
|
| 81 |
+
}
|