38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { randomBytes } from "node:crypto";
|
|
import { cookies } from "next/headers";
|
|
import { NextResponse } from "next/server";
|
|
import { requireAdmin } from "@/lib/auth/auth";
|
|
|
|
const COOKIE = "youtube-oauth-state";
|
|
|
|
export async function GET() {
|
|
await requireAdmin();
|
|
const clientId = process.env.YOUTUBE_CLIENT_ID;
|
|
const redirectUri = `${process.env.BASE_URL}/api/auth/youtube/callback`;
|
|
if (!clientId)
|
|
return NextResponse.json(
|
|
{ error: "YOUTUBE_CLIENT_ID is not configured." },
|
|
{ status: 500 },
|
|
);
|
|
const state = randomBytes(32).toString("hex");
|
|
const url = new URL("https://accounts.google.com/o/oauth2/v2/auth");
|
|
url.search = new URLSearchParams({
|
|
client_id: clientId,
|
|
redirect_uri: redirectUri,
|
|
response_type: "code",
|
|
access_type: "offline",
|
|
prompt: "consent",
|
|
scope: "https://www.googleapis.com/auth/youtube.upload",
|
|
state,
|
|
}).toString();
|
|
const response = NextResponse.redirect(url);
|
|
(await cookies()).set(COOKIE, state, {
|
|
httpOnly: true,
|
|
secure: process.env.NODE_ENV === "production",
|
|
sameSite: "lax",
|
|
maxAge: 600,
|
|
path: "/api/auth/youtube",
|
|
});
|
|
return response;
|
|
}
|