33 lines
920 B
TypeScript
33 lines
920 B
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
/**
|
|
* GET /api/auth/tiktok
|
|
* Redirects to TikTok OAuth authorization page.
|
|
* Visit this URL once to start the OAuth flow and get your refresh token.
|
|
*/
|
|
export async function GET() {
|
|
const clientKey = process.env.TIKTOK_CLIENT_KEY;
|
|
const redirectUri = `${process.env.BASE_URL}/api/auth/tiktok/callback`;
|
|
|
|
if (!clientKey) {
|
|
return NextResponse.json(
|
|
{ error: "TIKTOK_CLIENT_KEY not set in environment" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
const csrfState = Math.random().toString(36).substring(2);
|
|
|
|
const params = new URLSearchParams({
|
|
client_key: clientKey,
|
|
scope: "user.info.basic,user.info.profile,user.info.stats,video.list",
|
|
response_type: "code",
|
|
redirect_uri: redirectUri,
|
|
state: csrfState,
|
|
});
|
|
|
|
const authUrl = `https://www.tiktok.com/v2/auth/authorize/?${params.toString()}`;
|
|
|
|
return NextResponse.redirect(authUrl);
|
|
}
|