File size: 1,050 Bytes
d140e69
8be778a
d140e69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8be778a
 
d140e69
 
 
 
 
c796a00
d140e69
 
 
c796a00
d140e69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { NextResponse } from 'next/server';
import crypto from 'crypto';

/**
 * GET /api/auth/login
 * Redirects user to HuggingFace OAuth authorize URL.
 */
export async function GET(request) {
    const clientId = process.env.OAUTH_CLIENT_ID;
    if (!clientId) {
        return NextResponse.json(
            { error: 'OAuth not configured (missing OAUTH_CLIENT_ID). Set hf_oauth: true in Space metadata.' },
            { status: 500 }
        );
    }

    // Build redirect URI
    const host = process.env.SPACE_HOST
        ? `https://${process.env.SPACE_HOST}`
        : 'http://localhost:3000';
    const redirectUri = `${host}/api/auth/callback`;

    const state = crypto.randomBytes(16).toString('hex');

    const params = new URLSearchParams({
        client_id: clientId,
        redirect_uri: redirectUri,
        scope: 'openid profile',
        response_type: 'code',
        state,
    });

    const authorizeUrl = `https://huggingface.co/oauth/authorize?${params.toString()}`;
    return NextResponse.redirect(authorizeUrl);
}