@@ -8,7 +8,16 @@ const cookie_options = {
8
8
httpOnly : true ,
9
9
path : '/' ,
10
10
sameSite : 'Lax' ,
11
- maxAge : 60 * 60 * 24 * 365 // 1 year
11
+ // Set to 1 hour since that's typically how long access tokens last
12
+ maxAge : 60 * 60
13
+ } as const ;
14
+
15
+ const refresh_token_cookie_options = {
16
+ httpOnly : true ,
17
+ path : '/' ,
18
+ sameSite : 'Lax' ,
19
+ // Refresh tokens last much longer
20
+ maxAge : 60 * 60 * 24 * 365
12
21
} as const ;
13
22
14
23
export async function GET ( { url, cookies } ) {
@@ -19,15 +28,33 @@ export async function GET({ url, cookies }) {
19
28
GOOGLE_CLIENT_SECRET ,
20
29
PUBLIC_GOOGLE_REDIRECT
21
30
) ;
31
+
32
+ // Set up the correct scopes
33
+ oauth2Client . setCredentials ( {
34
+ scope : [ 'https://www.googleapis.com/auth/youtube.readonly' ]
35
+ } ) ;
36
+
22
37
const { tokens } = await oauth2Client . getToken ( code ) ;
23
- const token = tokens . access_token ;
38
+ console . log ( 'Received tokens:' , {
39
+ hasAccessToken : ! ! tokens . access_token ,
40
+ hasRefreshToken : ! ! tokens . refresh_token ,
41
+ expiryDate : tokens . expiry_date
42
+ } ) ;
43
+
44
+ if ( tokens . access_token ) {
45
+ // Store access token in a cookie named 'access_token'
46
+ cookies . set ( 'access_token' , tokens . access_token , cookie_options ) ;
24
47
25
- if ( token ) {
26
- cookies . set ( 'code' , token , cookie_options ) ;
48
+ // If we got a refresh token, store it for later
49
+ if ( tokens . refresh_token ) {
50
+ cookies . set ( 'refresh_token' , tokens . refresh_token , refresh_token_cookie_options ) ;
51
+ }
52
+ } else {
53
+ throw error ( 500 , 'No access token received' ) ;
27
54
}
28
55
} catch ( e ) {
29
- console . log ( e ) ;
56
+ console . error ( 'OAuth error:' , e ) ;
30
57
throw error ( 500 , 'Login Failed' ) ;
31
58
}
32
- redirect ( 302 , '/tools' ) ;
59
+ throw redirect ( 302 , '/tools' ) ;
33
60
}
0 commit comments