Handle invalid tokens
This commit is contained in:
		@@ -44,6 +44,13 @@ export class APIClient {
 | 
				
			|||||||
      data = await res.json();
 | 
					      data = await res.json();
 | 
				
			||||||
    else data = await res.blob();
 | 
					    else data = await res.blob();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Handle expired tokens
 | 
				
			||||||
 | 
					    if (res.status === 412) {
 | 
				
			||||||
 | 
					      AuthApi.RemoveAuthToken();
 | 
				
			||||||
 | 
					      // eslint-disable-next-line no-self-assign
 | 
				
			||||||
 | 
					      window.location.href = window.location.href;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!args.allowFail && !res.ok)
 | 
					    if (!args.allowFail && !res.ok)
 | 
				
			||||||
      throw new ApiError("Request failed!", res.status, data);
 | 
					      throw new ApiError("Request failed!", res.status, data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,7 +27,7 @@ export class AuthApi {
 | 
				
			|||||||
   * Check out whether user is signed in or not
 | 
					   * Check out whether user is signed in or not
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  static get SignedIn(): boolean {
 | 
					  static get SignedIn(): boolean {
 | 
				
			||||||
    return sessionStorage.getItem(TokenStateKey) !== null;
 | 
					    return localStorage.getItem(TokenStateKey) !== null;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static authStatus = atom(this.SignedIn);
 | 
					  static authStatus = atom(this.SignedIn);
 | 
				
			||||||
@@ -37,7 +37,7 @@ export class AuthApi {
 | 
				
			|||||||
   */
 | 
					   */
 | 
				
			||||||
  static get AuthToken(): string {
 | 
					  static get AuthToken(): string {
 | 
				
			||||||
    if (!this.SignedIn) throw new Error("User is not authenticated!");
 | 
					    if (!this.SignedIn) throw new Error("User is not authenticated!");
 | 
				
			||||||
    return sessionStorage.getItem(TokenStateKey)!;
 | 
					    return localStorage.getItem(TokenStateKey)!;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
@@ -99,7 +99,7 @@ export class AuthApi {
 | 
				
			|||||||
        return PasswordLoginResult.InvalidCredentials;
 | 
					        return PasswordLoginResult.InvalidCredentials;
 | 
				
			||||||
      case 200:
 | 
					      case 200:
 | 
				
			||||||
      case 201:
 | 
					      case 201:
 | 
				
			||||||
        sessionStorage.setItem(TokenStateKey, res.data.token);
 | 
					        localStorage.setItem(TokenStateKey, res.data.token);
 | 
				
			||||||
        return PasswordLoginResult.Success;
 | 
					        return PasswordLoginResult.Success;
 | 
				
			||||||
      default:
 | 
					      default:
 | 
				
			||||||
        return PasswordLoginResult.Error;
 | 
					        return PasswordLoginResult.Error;
 | 
				
			||||||
@@ -133,7 +133,7 @@ export class AuthApi {
 | 
				
			|||||||
      })
 | 
					      })
 | 
				
			||||||
    ).data;
 | 
					    ).data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sessionStorage.setItem(TokenStateKey, res.token);
 | 
					    localStorage.setItem(TokenStateKey, res.token);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
@@ -145,7 +145,14 @@ export class AuthApi {
 | 
				
			|||||||
      method: "GET",
 | 
					      method: "GET",
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sessionStorage.removeItem(TokenStateKey);
 | 
					    this.RemoveAuthToken();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * Remove auth token from storage
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  static RemoveAuthToken() {
 | 
				
			||||||
 | 
					    localStorage.removeItem(TokenStateKey);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user