@@ -260,49 +260,6 @@ export const sbhsAuthActions = {
260260 refresh : async ( ) => {
261261 // Check if valid state to refresh
262262 switch ( useSbhsAuthStore . getState ( ) . status ) {
263- case SbhsAuthStatus . LOGGED_IN :
264- // Set status to refreshing
265- useSbhsAuthStore . setState (
266- produce ( useSbhsAuthStore . getState ( ) , ( draft ) => {
267- draft . status = SbhsAuthStatus . REFRESHING ;
268- } )
269- ) ;
270- try {
271- const res = await fetch ( "/api/token" , {
272- method : "PATCH" ,
273- body : JSON . stringify ( {
274- client_id : config . client_id ,
275- } ) ,
276- headers : {
277- "Content-Type" : "application/json; charset=UTF-8" ,
278- } ,
279- } ) ;
280-
281- if ( ! res . ok ) {
282- throw new HTTPError ( res . status ) ;
283- }
284-
285- useSbhsAuthStore . setState (
286- produce ( useSbhsAuthStore . getState ( ) , ( draft ) => {
287- draft . status = SbhsAuthStatus . LOGGED_IN ;
288- } )
289- ) ;
290- } catch ( error ) {
291- useSbhsAuthStore . setState (
292- produce ( useSbhsAuthStore . getState ( ) , ( draft ) => {
293- draft . status = SbhsAuthStatus . EXPIRED ;
294- } )
295- ) ;
296- toast ( {
297- title :
298- "Something went wrong, try logging in and out if the issue persists." ,
299- description : error . message ,
300- status : "error" ,
301- isClosable : true ,
302- } ) ;
303- throw error ;
304- }
305- break ;
306263 // If we are already refreshing, wait for the refresh to finish
307264 case SbhsAuthStatus . REFRESHING : {
308265 const promise = new Promise < void > ( ( resolve , reject ) => {
@@ -317,13 +274,49 @@ export const sbhsAuthActions = {
317274 } ) ;
318275 return promise ;
319276 }
320- // Warn if invalid usage occurs
321277 default :
322- console . warn (
323- `Invalid usage of refresh action with auth status ${
324- useSbhsAuthStore . getState ( ) . status
325- } .`
326- ) ;
278+ if ( logged_in_states . includes ( useSbhsAuthStore . getState ( ) . status ) ) {
279+ // Set status to refreshing
280+ useSbhsAuthStore . setState (
281+ produce ( useSbhsAuthStore . getState ( ) , ( draft ) => {
282+ draft . status = SbhsAuthStatus . REFRESHING ;
283+ } )
284+ ) ;
285+ try {
286+ const res = await fetch ( "/api/token" , {
287+ method : "PATCH" ,
288+ body : JSON . stringify ( {
289+ client_id : config . client_id ,
290+ } ) ,
291+ headers : {
292+ "Content-Type" : "application/json; charset=UTF-8" ,
293+ } ,
294+ } ) ;
295+
296+ if ( ! res . ok ) {
297+ throw new HTTPError ( res . status ) ;
298+ }
299+
300+ useSbhsAuthStore . setState (
301+ produce ( useSbhsAuthStore . getState ( ) , ( draft ) => {
302+ draft . status = SbhsAuthStatus . LOGGED_IN ;
303+ } )
304+ ) ;
305+ } catch ( error ) {
306+ useSbhsAuthStore . setState (
307+ produce ( useSbhsAuthStore . getState ( ) , ( draft ) => {
308+ draft . status = SbhsAuthStatus . EXPIRED ;
309+ } )
310+ ) ;
311+ }
312+ } else {
313+ // Warn if invalid usage occurs
314+ console . warn (
315+ `Invalid usage of refresh action with auth status ${
316+ useSbhsAuthStore . getState ( ) . status
317+ } .`
318+ ) ;
319+ }
327320 }
328321 } ,
329322 fetchAuthenticated : async < TSbhsApiData > (
0 commit comments