initial commit
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 };
|
||||
@@ -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
@@ -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 },
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
Reference in New Issue
Block a user