initial commit

This commit is contained in:
Ivan Rezchikov
2026-06-18 22:37:03 +03:00
parent d5d42a2141
commit 6c89f21039
30 changed files with 1506 additions and 95 deletions
+11
View File
@@ -0,0 +1,11 @@
export const cookieConfig = {
httpOnly: true,
secure: process.env.NODE_ENV == "production",
sameSite: "lax" as const
}
export const pkceConfig = {
...cookieConfig,
path: "/",
maxAge: 60 * 10
}
+14
View File
@@ -0,0 +1,14 @@
import { cookies } from "next/headers";
import { cookieConfig } from "./cookieConfig";
export async function setTokens(accessToken: string, refreshToken: string, ttl: number) {
const store = await cookies();
store.set("access_token", accessToken, { ...cookieConfig, path: "/", maxAge: ttl });
store.set("refresh_token", refreshToken, { ...cookieConfig, path: "/", maxAge: ttl * 7 });
}
export async function deleteOAuthCookies() {
const store = await cookies();
store.delete("pkce_verifier");
store.delete("oauth_state");
}
+22
View File
@@ -0,0 +1,22 @@
import { SignJWT, jwtVerify } from "jose";
const secret = new TextEncoder().encode(process.env.JWT_SECRET!);
export async function signJwt(userId: string) {
return await new SignJWT({})
.setProtectedHeader({ alg: "HS256" })
.setSubject(userId)
.setIssuedAt()
.setExpirationTime(`${process.env.SESSION_MAX_AGE}s`)
.sign(secret);
}
export async function verifyJwt(token: string) {
try {
const { payload } = await jwtVerify(token, secret);
return payload;
} catch {
return null;
}
}
+10
View File
@@ -0,0 +1,10 @@
import "dotenv/config";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "@/lib/generated/prisma/client";
const connectionString = `${process.env.DATABASE_URL}`;
const adapter = new PrismaPg({ connectionString });
const prisma = new PrismaClient({ adapter });
export { prisma };
+9
View File
@@ -0,0 +1,9 @@
"use server";
import { cookies } from "next/headers";
import { cookieConfig } from "./cookieConfig";
export async function setSessionCookie(token: string) {
const store = await cookies();
store.set("session", token, { ...cookieConfig, path: "/", maxAge: parseInt(process.env.SESSION_MAX_AGE!) });
}
+17
View File
@@ -0,0 +1,17 @@
import { cookies } from "next/headers";
import { prisma } from "@/lib/prisma";
import { verifyJwt } from "@/lib/jwt";
export async function getCurrentUser() {
const store = await cookies();
const token = store.get("session")?.value;
if (!token) return null;
const payload = await verifyJwt(token);
if (!payload?.sub) return null;
return prisma.user.findUnique({
where: { id: payload.sub as string },
});
}
+6
View File
@@ -0,0 +1,6 @@
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}