hugex-gh / app /lib /session.server.ts
drbh
feat: refactor for linking
dc06026
import { createCookieSessionStorage } from "@remix-run/node";
export { getSession, commitSession, destroySession };
export interface GitHubUserInfo {
userId: string;
login: string;
name?: string;
email?: string;
avatar_url?: string;
}
export interface HuggingFaceUserInfo {
username: string;
fullName?: string;
email?: string;
avatarUrl?: string;
}
export interface UserSession {
github?: GitHubUserInfo;
huggingface?: HuggingFaceUserInfo;
isLinked: boolean;
linkedAt?: string;
}
const sessionSecret = process.env.SESSION_SECRET;
if (!sessionSecret) {
throw new Error('SESSION_SECRET environment variable is required');
}
const { getSession, commitSession, destroySession } = createCookieSessionStorage({
cookie: {
name: "__session",
httpOnly: true,
maxAge: 60 * 60 * 24 * 30, // 30 days
path: "/",
sameSite: "lax",
secrets: [sessionSecret],
secure: process.env.NODE_ENV === "production",
},
});
export async function requireUserSession(request: Request): Promise<UserSession> {
const session = await getSession(request.headers.get("Cookie"));
const userSession = session.get("user");
if (!userSession) {
throw new Response("Unauthorized", { status: 401 });
}
return userSession;
}
export async function getUserSession(request: Request): Promise<UserSession | null> {
const session = await getSession(request.headers.get("Cookie"));
return session.get("user") || null;
}