-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
75 lines (60 loc) · 2.26 KB
/
Copy pathroute.ts
File metadata and controls
75 lines (60 loc) · 2.26 KB
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
71
72
73
74
75
import { NextRequest, NextResponse } from "next/server"
import { cookies } from "next/headers"
export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams
const code = searchParams.get("code")
const state = searchParams.get("state")
const error = searchParams.get("error")
const origin = request.nextUrl.origin
if (error) {
console.error("GitHub OAuth error:", error)
return NextResponse.redirect(new URL("/?github_error=" + error, origin))
}
const cookieStore = await cookies()
const savedState = cookieStore.get("github_oauth_state")?.value
if (!state || state !== savedState) {
console.error("[GitHub] State mismatch")
return NextResponse.redirect(new URL("/?github_error=state_mismatch", origin))
}
cookieStore.delete("github_oauth_state")
if (!code) {
return NextResponse.redirect(new URL("/?github_error=no_code", origin))
}
try {
const response = await fetch("https://github.com/login/oauth/access_token", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
client_id: process.env.GITHUB_CLIENT_ID!,
client_secret: process.env.GITHUB_CLIENT_SECRET!,
code,
redirect_uri: `${origin}/api/auth/github/callback`,
}),
})
const data = await response.json()
if (data.error) {
console.error("GitHub token exchange error:", data.error)
return NextResponse.redirect(new URL("/?github_error=" + data.error, origin))
}
const accessToken = data.access_token
if (!accessToken) {
console.error("No access token in GitHub response")
return NextResponse.redirect(new URL("/?github_error=no_access_token", origin))
}
cookieStore.set("github_token", accessToken, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
maxAge: 60 * 60 * 24 * 365,
path: "/",
})
console.log("[GitHub] OAuth successful, token stored")
return NextResponse.redirect(new URL("/?github_connected=true", origin))
} catch (error) {
console.error("GitHub OAuth error:", error)
return NextResponse.redirect(new URL("/?github_error=exchange_failed", origin))
}
}