File size: 1,511 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Google OAuth callback proxy on the docdoe.in domain.
//
// WHY: Google's consent screen shows the host of the OAuth redirect_uri. When
// that points straight at the backend (asnannp-docdoeai.hf.space) the screen
// reads "to continue to asnannp-docdoeai.hf.space". Registering THIS route as
// the redirect_uri makes Google show "to continue to www.docdoe.in" instead.
//
// FLOW:
//   Google -> https://www.docdoe.in/api/auth/google/callback?code=..&state=..
//          -> 302 to <backend>/auth/google/callback?code=..&state=..
//          -> backend exchanges the code (redirect_uri must equal THIS url)
//          -> backend 302s to /auth/google/callback#access_token=.. (frontend page)
//
// REQUIRED config to activate (do these together):
//   * HF Space env: GOOGLE_OAUTH_REDIRECT_URI=https://www.docdoe.in/api/auth/google/callback
//   * Google Console -> Clients -> Authorized redirect URIs: add the same URL

import { NextResponse } from "next/server";

const BACKEND_BASE = (process.env.NEXT_PUBLIC_API_BASE_URL || "https://asnannp-docdoeai.hf.space").replace(/\/$/, "");

export const dynamic = "force-dynamic";

export function GET(request: Request) {
  const incoming = new URL(request.url);
  const target = new URL(`${BACKEND_BASE}/auth/google/callback`);
  // Forward exactly the params Google sent (code, state, error, scope, ...).
  incoming.searchParams.forEach((value, key) => {
    target.searchParams.set(key, value);
  });
  return NextResponse.redirect(target.toString(), 302);
}