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" ;
28
39export const frameworkSettings : {
410 framework : string ;
@@ -13,6 +19,7 @@ interface ExchangeAuthCodeParams {
1319 domain : string ;
1420 clientId : string ;
1521 redirectURL : string ;
22+ autoReferesh ?: boolean ;
1623}
1724
1825interface ExchangeAuthCodeResult {
@@ -28,6 +35,7 @@ export const exchangeAuthCode = async ({
2835 domain,
2936 clientId,
3037 redirectURL,
38+ autoReferesh = false ,
3139} : ExchangeAuthCodeParams ) : Promise < ExchangeAuthCodeResult > => {
3240 const state = urlParams . get ( "state" ) ;
3341 const code = urlParams . get ( "code" ) ;
@@ -69,15 +77,16 @@ export const exchangeAuthCode = async ({
6977 StorageKeys . codeVerifier ,
7078 ) ) as string ;
7179
80+
7281 const headers : {
7382 "Content-type" : string ;
74- // "Cache-Control": string;
75- // Pragma: string;
83+ "Cache-Control" : string ;
84+ Pragma : string ;
7685 "Kinde-SDK" ?: string ;
7786 } = {
7887 "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" ,
8190 } ;
8291
8392 if ( frameworkSettings . framework ) {
@@ -106,11 +115,13 @@ export const exchangeAuthCode = async ({
106115 error : `Token exchange failed: ${ response . status } - ${ errorText } ` ,
107116 } ;
108117 }
118+ clearRefreshTimer ( )
109119
110120 const data : {
111121 access_token : string ;
112122 id_token : string ;
113123 refresh_token : string ;
124+ expires_in : number ;
114125 } = await response . json ( ) ;
115126
116127 const secureStore = getActiveStorage ( ) ;
@@ -120,7 +131,17 @@ export const exchangeAuthCode = async ({
120131 [ StorageKeys . refreshToken ] : data . refresh_token ,
121132 } ) ;
122133
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+ ) ;
124145
125146 // Clear all url params
126147 const cleanUrl = ( url : URL ) : URL => {
0 commit comments