cg / hackerproducts

64 files · 5,803 lines · 161.0 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.

Quick jump: page.tsx / page.tsx / globals.css / schema.sql / package.json

root / src / app / api / auth / github / route.ts

src/app/api/auth/github/route.ts · 36 lines · 1.2 KB← back to folder
1import crypto from "node:crypto";
2import { cookies } from "next/headers";
3import { NextResponse } from "next/server";
4import { getSiteUrl } from "@/lib/auth";
5
6export async function GET(request: Request) {
7 const clientId = process.env.GITHUB_CLIENT_ID;
8 if (!clientId || !process.env.GITHUB_CLIENT_SECRET) {
9 return NextResponse.redirect(new URL("/login?error=github_not_configured", getSiteUrl()));
10 }
11
12 const url = new URL(request.url);
13 const next = url.searchParams.get("next") || "/";
14 const safeNext = next.startsWith("/") && !next.startsWith("//") ? next : "/";
15
16 const state = crypto.randomBytes(24).toString("hex");
17 const cookieStore = await cookies();
18 cookieStore.set("hp_oauth_state", JSON.stringify({ state, next: safeNext }), {
19 httpOnly: true,
20 sameSite: "lax",
21 secure: process.env.NODE_ENV === "production",
22 path: "/",
23 maxAge: 600,
24 });
25
26 const redirectUri = `${getSiteUrl()}/api/auth/github/callback`;
27 const params = new URLSearchParams({
28 client_id: clientId,
29 redirect_uri: redirectUri,
30 scope: "read:user user:email",
31 state,
32 });
33
34 return NextResponse.redirect(`https://github.com/login/oauth/authorize?${params.toString()}`);
35}
36
cg / hackerproducts — hacker products