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 / callback / route.ts
src/app/api/auth/google/callback/route.ts · 81 lines · 2.6 KB← back to folder
| 1 | import { cookies } from "next/headers"; |
| 2 | import { NextResponse } from "next/server"; |
| 3 | import { createSession, getSiteUrl, upsertOAuthUser } from "@/lib/auth"; |
| 4 | |
| 5 | export async function GET(request: Request) { |
| 6 | const siteUrl = getSiteUrl(); |
| 7 | const url = new URL(request.url); |
| 8 | const code = url.searchParams.get("code"); |
| 9 | const state = url.searchParams.get("state"); |
| 10 | |
| 11 | const cookieStore = await cookies(); |
| 12 | const rawStateCookie = cookieStore.get("hp_oauth_state")?.value; |
| 13 | cookieStore.delete("hp_oauth_state"); |
| 14 | |
| 15 | if (!code || !state || !rawStateCookie) { |
| 16 | return NextResponse.redirect(new URL("/login?error=oauth_failed", siteUrl)); |
| 17 | } |
| 18 | |
| 19 | let parsedState: { state: string; next: string }; |
| 20 | try { |
| 21 | parsedState = JSON.parse(rawStateCookie); |
| 22 | } catch { |
| 23 | return NextResponse.redirect(new URL("/login?error=oauth_failed", siteUrl)); |
| 24 | } |
| 25 | |
| 26 | if (parsedState.state !== state) { |
| 27 | return NextResponse.redirect(new URL("/login?error=invalid_state", siteUrl)); |
| 28 | } |
| 29 | |
| 30 | const tokenRes = await fetch("https://oauth2.googleapis.com/token", { |
| 31 | method: "POST", |
| 32 | headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
| 33 | body: new URLSearchParams({ |
| 34 | client_id: process.env.GOOGLE_CLIENT_ID ?? "", |
| 35 | client_secret: process.env.GOOGLE_CLIENT_SECRET ?? "", |
| 36 | code, |
| 37 | grant_type: "authorization_code", |
| 38 | redirect_uri: `${siteUrl}/api/auth/google/callback`, |
| 39 | }), |
| 40 | }); |
| 41 | |
| 42 | if (!tokenRes.ok) { |
| 43 | return NextResponse.redirect(new URL("/login?error=token_exchange_failed", siteUrl)); |
| 44 | } |
| 45 | |
| 46 | const tokenJson = (await tokenRes.json()) as { access_token?: string }; |
| 47 | if (!tokenJson.access_token) { |
| 48 | return NextResponse.redirect(new URL("/login?error=token_missing", siteUrl)); |
| 49 | } |
| 50 | |
| 51 | const userRes = await fetch("https://www.googleapis.com/oauth2/v2/userinfo", { |
| 52 | headers: { Authorization: `Bearer ${tokenJson.access_token}` }, |
| 53 | }); |
| 54 | |
| 55 | if (!userRes.ok) { |
| 56 | return NextResponse.redirect(new URL("/login?error=profile_fetch_failed", siteUrl)); |
| 57 | } |
| 58 | |
| 59 | const gUser = (await userRes.json()) as { |
| 60 | id: string; |
| 61 | email: string; |
| 62 | name?: string; |
| 63 | picture?: string; |
| 64 | }; |
| 65 | |
| 66 | const preferredUsername = gUser.email ? gUser.email.split("@")[0] : gUser.name ?? "maker"; |
| 67 | |
| 68 | const user = await upsertOAuthUser({ |
| 69 | provider: "google", |
| 70 | providerAccountId: String(gUser.id), |
| 71 | email: gUser.email ?? null, |
| 72 | name: gUser.name ?? null, |
| 73 | preferredUsername, |
| 74 | avatarUrl: gUser.picture ?? null, |
| 75 | }); |
| 76 | |
| 77 | await createSession(user.id); |
| 78 | const safeNext = parsedState.next.startsWith("/") && !parsedState.next.startsWith("//") ? parsedState.next : "/"; |
| 79 | return NextResponse.redirect(new URL(safeNext, siteUrl)); |
| 80 | } |
| 81 |