Initial commit
This commit is contained in:
30
app/auth/google/callback/route.ts
Normal file
30
app/auth/google/callback/route.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { getAuth } from "@/lib/auth";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const auth = await getAuth();
|
||||
const response = await auth.handler(req);
|
||||
|
||||
// Get redirect URL from query params if provided
|
||||
const redirectUrl = req.nextUrl.searchParams.get("redirect") || "/account/webinars";
|
||||
|
||||
// If response is a redirect, extract location and modify it
|
||||
if (response.status >= 300 && response.status < 400) {
|
||||
const location = response.headers.get("location");
|
||||
if (location) {
|
||||
// If it's redirecting to a callback page, preserve the redirect param
|
||||
return NextResponse.redirect(new URL(`/auth-callback?redirect=${encodeURIComponent(redirectUrl)}`, req.url));
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise return auth response as is, then frontend will handle redirect
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error("Google callback error:", error);
|
||||
return new Response("Authentication failed", { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
51
app/auth/google/route.ts
Normal file
51
app/auth/google/route.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { loadSystemConfig } from "@/lib/system-config";
|
||||
import crypto from "crypto";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const systemConfig = await loadSystemConfig();
|
||||
const { googleAuth } = systemConfig;
|
||||
|
||||
if (!googleAuth?.clientId || !googleAuth?.clientSecret) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: "Google OAuth not configured" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Generate CSRF token
|
||||
const state = crypto.randomBytes(32).toString("hex");
|
||||
|
||||
// Store state in cookie for verification
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.set("oauth_state", state, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
sameSite: "lax",
|
||||
maxAge: 600, // 10 minutes
|
||||
});
|
||||
|
||||
// Build Google OAuth URL
|
||||
const params = new URLSearchParams({
|
||||
client_id: googleAuth.clientId,
|
||||
redirect_uri: `${process.env.APP_BASE_URL || "http://localhost:3001"}/auth/google/callback`,
|
||||
response_type: "code",
|
||||
scope: "openid email profile",
|
||||
state,
|
||||
access_type: "offline",
|
||||
prompt: "consent",
|
||||
});
|
||||
|
||||
const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
|
||||
|
||||
return NextResponse.redirect(authUrl);
|
||||
} catch (error) {
|
||||
console.error("Google OAuth error:", error);
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: "Failed to initiate OAuth" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user