cg / hackerproducts
64 files · 5,803 lines · 161.0 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.
Quick jump: page.tsxpage.tsxglobals.cssschema.sqlpackage.json
root / src / app / api / auth / google / route.ts
src/app/api/auth/google/route.ts · 38 lines · 1.2 KB← back to folder
| 1 | import crypto from "node:crypto"; |
| 2 | import { cookies } from "next/headers"; |
| 3 | import { NextResponse } from "next/server"; |
| 4 | import { getSiteUrl } from "@/lib/auth"; |
| 5 | |
| 6 | export async function GET(request: Request) { |
| 7 | const clientId = process.env.GOOGLE_CLIENT_ID; |
| 8 | if (!clientId || !process.env.GOOGLE_CLIENT_SECRET) { |
| 9 | return NextResponse.redirect(new URL("/login?error=google_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/google/callback`; |
| 27 | const params = new URLSearchParams({ |
| 28 | client_id: clientId, |
| 29 | redirect_uri: redirectUri, |
| 30 | response_type: "code", |
| 31 | scope: "openid email profile", |
| 32 | state, |
| 33 | prompt: "select_account", |
| 34 | }); |
| 35 | |
| 36 | return NextResponse.redirect(`https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`); |
| 37 | } |
| 38 |