1
1
import createDebug from 'debug' ;
2
2
import {
3
3
AuthorizationError ,
4
- type AuthenticateOptions ,
4
+ type AuthOptions ,
5
+ type SessionStorage ,
5
6
Strategy ,
6
7
type StrategyVerifyCallback
7
8
} from '@svelte-dev/auth' ;
@@ -133,14 +134,13 @@ export class OAuth2Strategy<
133
134
this . useBasicAuthenticationHeader = options . useBasicAuthenticationHeader ?? false ;
134
135
}
135
136
136
- async authenticate ( event : RequestEvent , options : AuthenticateOptions ) : Promise < User > {
137
- const { request, cookies } = event ;
137
+ async authenticate ( event : RequestEvent , options : AuthOptions ) : Promise < User | void > {
138
+ const { request } = event ;
139
+ const session = ( event . locals as any ) . session as SessionStorage ;
138
140
debug ( 'Request URL' , request . url ) ;
139
141
let url = new URL ( request . url ) ;
140
142
141
- let user : User | null = cookies . get ( options . sessionKey )
142
- ? JSON . parse ( cookies . get ( options . sessionKey ) ! )
143
- : null ;
143
+ let user = session . get ( 'user' ) as User ;
144
144
145
145
// User is already authenticated
146
146
if ( user ) {
@@ -157,7 +157,7 @@ export class OAuth2Strategy<
157
157
debug ( 'Redirecting to callback URL' ) ;
158
158
let state = this . generateState ( ) ;
159
159
debug ( 'State' , state ) ;
160
- cookies . set ( this . sessionStateKey , state ) ;
160
+ await session . set ( 'state' , state ) ;
161
161
throw redirect ( 307 , this . getAuthorizationURL ( request , state ) . toString ( ) ) ;
162
162
}
163
163
@@ -174,7 +174,7 @@ export class OAuth2Strategy<
174
174
) ;
175
175
}
176
176
177
- let stateSession = cookies . get ( this . sessionStateKey ) ;
177
+ let stateSession = session . get ( 'state' ) ;
178
178
debug ( 'State from session' , stateSession ) ;
179
179
if ( ! stateSession ) {
180
180
return await this . failure (
@@ -187,7 +187,7 @@ export class OAuth2Strategy<
187
187
188
188
if ( stateSession === stateUrl ) {
189
189
debug ( 'State is valid' ) ;
190
- cookies . delete ( this . sessionStateKey ) ;
190
+ await session . unset ( 'state' ) ;
191
191
} else {
192
192
return await this . failure (
193
193
"State doesn't match." ,
0 commit comments