Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
151 lines (130 loc) · 4.7 KB
/
Copy pathproxy.ts
File metadata and controls
151 lines (130 loc) · 4.7 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import {
APP_SESSION_COOKIE,
getSessionCookieOptions,
GOOGLE_TOKEN_COOKIE,
} from "@/lib/gdrive/google-oauth";
// Helper to decode JWT and check expiry (without verification)
function isTokenExpired(accessToken: string, bufferSeconds = 300): boolean {
try {
// JWT format: header.payload.signature
const parts = accessToken.split(".");
if (parts.length !== 3) return true;
const payload = JSON.parse(atob(parts[1]));
const exp = payload.exp;
if (!exp) return true;
// Check if token expires within bufferSeconds (default 5 minutes)
const now = Math.floor(Date.now() / 1000);
return exp < now + bufferSeconds;
} catch {
// If we can't parse the token, assume it might be expired
return true;
}
}
async function refreshAccessToken(
refreshToken: string,
clientId: string,
clientSecret: string
): Promise<{ access_token: string; expires_in: number } | null> {
try {
const response = await fetch("https://oauth2.googleapis.com/token", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
client_id: clientId,
client_secret: clientSecret,
refresh_token: refreshToken,
grant_type: "refresh_token",
}),
});
if (!response.ok) {
console.error("Token refresh failed:", await response.text());
return null;
}
return await response.json();
} catch (error) {
console.error("Token refresh error:", error);
return null;
}
}
export async function proxy(request: NextRequest) {
const appSession = request.cookies.get(APP_SESSION_COOKIE)?.value;
const tokensStr = request.cookies.get(GOOGLE_TOKEN_COOKIE)?.value;
const clientId = request.cookies.get("gdrive_client_id")?.value;
const clientSecret = request.cookies.get("gdrive_client_secret")?.value;
const { pathname } = request.nextUrl;
// If user is signed in to the app and is on the root page, redirect to dashboard
if (appSession && pathname === "/") {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
// If user is not signed into the app, protect dashboard routes.
if (!appSession && pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/", request.url));
}
// For dashboard routes, proactively refresh expired tokens
if (pathname.startsWith("/dashboard") && tokensStr) {
try {
if (!clientId || !clientSecret) {
const response = NextResponse.next();
response.cookies.delete(GOOGLE_TOKEN_COOKIE);
return response;
}
const tokens = JSON.parse(tokensStr);
// Check if access token is expired or about to expire
if (tokens.access_token && isTokenExpired(tokens.access_token)) {
console.log("🔄 Proxy: Proactively refreshing expired access token...");
if (!tokens.refresh_token) {
console.error("No refresh token available");
const response = NextResponse.next();
response.cookies.delete(GOOGLE_TOKEN_COOKIE);
return response;
}
const newTokenData = await refreshAccessToken(
tokens.refresh_token,
clientId,
clientSecret
);
if (!newTokenData) {
console.error("Failed to refresh token, redirecting to login");
const response = NextResponse.next();
response.cookies.delete(GOOGLE_TOKEN_COOKIE);
return response;
}
// Update tokens with new access token
const updatedTokens = {
...tokens,
access_token: newTokenData.access_token,
};
console.log("✅ Proxy: Token refreshed successfully");
// Create response and set updated cookie
const response = NextResponse.next();
response.cookies.set(
GOOGLE_TOKEN_COOKIE,
JSON.stringify(updatedTokens),
getSessionCookieOptions(60 * 60 * 24 * 30)
);
return response;
}
} catch (error) {
console.error("Proxy auth error:", error);
const response = NextResponse.next();
response.cookies.delete(GOOGLE_TOKEN_COOKIE);
return response;
}
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - Static files (images, etc.)
*/
"/((?!api|auth|_next/static|_next/image|favicon.ico|.well-known|oauth2callback|.*\\.png$|.*\\.svg$|.*\\.jpg$|.*\\.jpeg$|.*\\.gif$|.*\\.ico$|.*\\.webp$).*)",
],
};