@@ -75,18 +75,18 @@ const {
75
75
return result . id ! ;
76
76
} ,
77
77
async readData ( id ) {
78
- const result = await db
78
+ const result = ( await db
79
79
. selectFrom ( "sessions" )
80
80
. where ( "id" , "=" , id )
81
81
. selectAll ( )
82
- . executeTakeFirst ( ) ;
83
- return result ? .data ? JSON . parse ( result . data ) : null ;
82
+ . executeTakeFirst ( ) ) ?? { data : { } as any , expires : undefined } ;
83
+ return result . data ;
84
84
} ,
85
85
async updateData ( id , data , expires ) {
86
86
await db
87
87
. updateTable ( "sessions" )
88
88
. set ( "data" , JSON . stringify ( data ) )
89
- . set ( "expires" , expires ! . toString ( ) )
89
+ . set ( "expires" , expires ? .toString ( ) || null )
90
90
. where ( "id" , "=" , id )
91
91
. execute ( ) ;
92
92
} ,
@@ -158,9 +158,8 @@ export async function getUser(request: Request) {
158
158
if ( userId === undefined ) return null ;
159
159
160
160
const user = await getUserById ( userId ) ;
161
- if ( user ) return user ;
162
-
163
- throw await logout ( request ) ;
161
+ if ( ! user ) throw await logout ( request ) ;
162
+ return user ;
164
163
}
165
164
166
165
export async function requireUserId (
@@ -188,14 +187,21 @@ const OAUTH_REDIRECT = "http://localhost:3000/discord-oauth";
188
187
189
188
export async function initOauthLogin ( {
190
189
request,
190
+ redirectTo,
191
191
} : {
192
192
request : Request ;
193
- redirectTo : string ;
193
+ redirectTo ? : string ;
194
194
} ) {
195
195
const dbSession = await getDbSession ( request . headers . get ( "Cookie" ) ) ;
196
196
197
197
const state = randomUUID ( ) ;
198
198
dbSession . set ( "state" , state ) ;
199
+ if ( redirectTo ) {
200
+ dbSession . set ( "redirectTo" , redirectTo ) ;
201
+ }
202
+ const cookie = await commitDbSession ( dbSession , {
203
+ maxAge : 60 * 60 * 1 , // 1 hour
204
+ } ) ;
199
205
return redirect (
200
206
authorization . authorizeURL ( {
201
207
redirect_uri : OAUTH_REDIRECT ,
@@ -204,21 +210,17 @@ export async function initOauthLogin({
204
210
} ) ,
205
211
{
206
212
headers : {
207
- "Set-Cookie" : await commitDbSession ( dbSession , {
208
- maxAge : 60 * 60 * 1 , // 1 hour
209
- } ) ,
213
+ "Set-Cookie" : cookie ,
210
214
} ,
211
215
} ,
212
216
) ;
213
217
}
214
218
215
- export async function completeOauthLogin ( request : Request ) {
216
- const url = new URL ( request . url ) ;
217
- const code = url . searchParams . get ( "code" ) ;
218
- if ( ! code ) {
219
- throw json ( { message : `Discord didn't send an auth code` } , 500 ) ;
220
- }
221
-
219
+ export async function completeOauthLogin (
220
+ code : string ,
221
+ reqCookie : string ,
222
+ state ?: string ,
223
+ ) {
222
224
const token = await authorization . getToken ( {
223
225
scope : SCOPE ,
224
226
code,
@@ -245,15 +247,14 @@ export async function completeOauthLogin(request: Request) {
245
247
}
246
248
247
249
const [ cookieSession , dbSession ] = await Promise . all ( [
248
- getCookieSession ( request . headers . get ( "Cookie" ) ) ,
249
- getDbSession ( request . headers . get ( "Cookie" ) ) ,
250
+ getCookieSession ( reqCookie ) ,
251
+ getDbSession ( reqCookie ) ,
250
252
] ) ;
251
253
252
- // 401 if the state arg doesn't match
253
- const state = url . searchParams . get ( "state" ) ;
254
- console . log ( { state, dbState : dbSession . get ( "state" ) } ) ;
254
+ // Redirect to login if the state arg doesn't match
255
255
if ( dbSession . get ( "state" ) !== state ) {
256
- throw redirect ( "/login" , 401 ) ;
256
+ console . error ( "DB state didn’t match cookie state" ) ;
257
+ throw redirect ( "/login" ) ;
257
258
}
258
259
259
260
cookieSession . set ( USER_SESSION_KEY , userId ) ;
@@ -269,7 +270,7 @@ export async function completeOauthLogin(request: Request) {
269
270
headers . append ( "Set-Cookie" , cookie ) ;
270
271
headers . append ( "Set-Cookie" , dbCookie ) ;
271
272
272
- return redirect ( "/" , { headers } ) ;
273
+ return redirect ( dbSession . get ( "redirectTo" ) ?? "/" , { headers } ) ;
273
274
}
274
275
275
276
export async function refreshSession ( request : Request ) {
0 commit comments