1
- import { getActiveStorage , getInsecureStorage , StorageKeys } from "../main" ;
1
+ import {
2
+ getActiveStorage ,
3
+ getInsecureStorage ,
4
+ refreshToken ,
5
+ StorageKeys ,
6
+ } from "../main" ;
7
+ import { clearRefreshTimer , setRefreshTimer } from "./refreshTimer" ;
2
8
3
9
export const frameworkSettings : {
4
10
framework : string ;
@@ -13,6 +19,7 @@ interface ExchangeAuthCodeParams {
13
19
domain : string ;
14
20
clientId : string ;
15
21
redirectURL : string ;
22
+ autoReferesh ?: boolean ;
16
23
}
17
24
18
25
interface ExchangeAuthCodeResult {
@@ -28,6 +35,7 @@ export const exchangeAuthCode = async ({
28
35
domain,
29
36
clientId,
30
37
redirectURL,
38
+ autoReferesh = false ,
31
39
} : ExchangeAuthCodeParams ) : Promise < ExchangeAuthCodeResult > => {
32
40
const state = urlParams . get ( "state" ) ;
33
41
const code = urlParams . get ( "code" ) ;
@@ -69,15 +77,16 @@ export const exchangeAuthCode = async ({
69
77
StorageKeys . codeVerifier ,
70
78
) ) as string ;
71
79
80
+
72
81
const headers : {
73
82
"Content-type" : string ;
74
- // "Cache-Control": string;
75
- // Pragma: string;
83
+ "Cache-Control" : string ;
84
+ Pragma : string ;
76
85
"Kinde-SDK" ?: string ;
77
86
} = {
78
87
"Content-type" : "application/x-www-form-urlencoded; charset=UTF-8" ,
79
- // "Cache-Control": "no-store",
80
- // Pragma: "no-cache",
88
+ "Cache-Control" : "no-store" ,
89
+ Pragma : "no-cache" ,
81
90
} ;
82
91
83
92
if ( frameworkSettings . framework ) {
@@ -106,11 +115,13 @@ export const exchangeAuthCode = async ({
106
115
error : `Token exchange failed: ${ response . status } - ${ errorText } ` ,
107
116
} ;
108
117
}
118
+ clearRefreshTimer ( )
109
119
110
120
const data : {
111
121
access_token : string ;
112
122
id_token : string ;
113
123
refresh_token : string ;
124
+ expires_in : number ;
114
125
} = await response . json ( ) ;
115
126
116
127
const secureStore = getActiveStorage ( ) ;
@@ -120,7 +131,17 @@ export const exchangeAuthCode = async ({
120
131
[ StorageKeys . refreshToken ] : data . refresh_token ,
121
132
} ) ;
122
133
123
- await activeStorage . removeItems ( StorageKeys . state , StorageKeys . nonce , StorageKeys . codeVerifier ) ;
134
+ if ( autoReferesh ) {
135
+ setRefreshTimer ( data . expires_in * 1000 , async ( ) => {
136
+ refreshToken ( domain , clientId ) ;
137
+ } ) ;
138
+ }
139
+
140
+ await activeStorage . removeItems (
141
+ StorageKeys . state ,
142
+ StorageKeys . nonce ,
143
+ StorageKeys . codeVerifier ,
144
+ ) ;
124
145
125
146
// Clear all url params
126
147
const cleanUrl = ( url : URL ) : URL => {
0 commit comments