File size: 2,370 Bytes
522ddd5
54bc655
 
522ddd5
 
ac7030c
522ddd5
 
 
54bc655
ac7030c
8715c49
522ddd5
8715c49
 
522ddd5
8715c49
522ddd5
 
 
 
 
 
 
 
 
 
 
ac7030c
522ddd5
c085699
 
 
 
 
 
522ddd5
ac7030c
54bc655
 
 
ac7030c
8715c49
 
 
 
522ddd5
8715c49
522ddd5
 
 
 
 
 
 
 
 
 
 
ac7030c
522ddd5
8715c49
 
 
 
 
522ddd5
 
ac7030c
54bc655
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import querystring from "node:querystring"
import { NextResponse, NextRequest } from "next/server"

const defaultState = JSON.stringify({
  nonce: "",
  redirectUri: `${process.env.NEXT_PUBLIC_DOMAIN}/api/login`,
  state: JSON.stringify({ redirectTo: "/" })
})

export async function GET(req: NextRequest) {
 // we are going to pass the whole thing unchanged to the AiTube frontend
 const params = req.url.split("/api/login").pop() || ""

  
 const query = querystring.parse(params)

  console.log("Received GET /api/login:", params)
  console.log(query)


  // we are only interested in our own data, actually
  // const code = `${query.code || ""}` // <-- ignored, we will send it as-is
  const {
    // nonce, // <-- ignored, we will send it as-is
    // redirectUri, // <-- ignored, we will send it as-is
    state // <-- this is defined by us!
  } = JSON.parse(`${query.state || defaultState}`)

  // this is the path of the AiTube page which the user was browser
  // eg. this can be /account, /, or nothing
  // const redirectTo = `${state.redirectTo || "/"}`

  // for now we have to always return to /account, since this is where
  // the oauth "finisher" code resides
  const redirectTo = "/account"


  return NextResponse.redirect(`${process.env.NEXT_PUBLIC_DOMAIN}${redirectTo}${params}`)
}

export async function POST(req: NextRequest, res: NextResponse) {
  // we are going to pass the whole thing unchanged to the AiTube frontend
  const params = req.url.split("/api/login").pop() || ""

  
  const query = querystring.parse(params)

  console.log("Received POST /api/login:", params)
  console.log(query)


  // we are only interested in our own data, actually
  // const code = `${query.code || ""}` // <-- ignored, we will send it as-is
  const {
    // nonce, // <-- ignored, we will send it as-is
    // redirectUri, // <-- ignored, we will send it as-is
    state // <-- this is defined by us!
  } = JSON.parse(`${query.state || defaultState}`)

  // this is the path of the AiTube page which the user was browser
  // eg. this can be /account, /, or nothing
  // const redirectTo = `${state.redirectTo || "/"}`

  // for now we have to always return to /account, since this is where
  // the oauth "finisher" code resides
  const redirectTo = "/account"


  return NextResponse.redirect(`${process.env.NEXT_PUBLIC_DOMAIN}${redirectTo}${params}`)
}