From d56eeb50626e6f875ab33d3bb7fbe12d8c8785b5 Mon Sep 17 00:00:00 2001 From: jvyden Date: Sun, 5 Nov 2023 18:05:57 -0500 Subject: [PATCH] Cache user in localStorage --- src/app/api/auth.service.ts | 12 ++++++ src/app/api/token-storage.service.ts | 59 ++++++++++++++++++---------- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/app/api/auth.service.ts b/src/app/api/auth.service.ts index e8beb35..6caf257 100644 --- a/src/app/api/auth.service.ts +++ b/src/app/api/auth.service.ts @@ -33,9 +33,16 @@ export class AuthService { this.userWatcher = new EventEmitter(); const storedToken: string | null = this.tokenStorage.GetStoredGameToken(); + const storedUser: ExtendedUser | null = this.tokenStorage.GetStoredUser(); this._loggedIn = storedToken !== null; if (storedToken) { + if(storedUser) { + this._userId = storedUser.userId; + this.user = storedUser; + this.userWatcher.emit(this.user); + } + this.GetMyUser(() => { // only subscribe after getting user this.userWatcher.subscribe((user) => this.onUserUpdate(user)); @@ -168,6 +175,7 @@ export class AuthService { private GetMyUser(callback: Function | null = null, tryingToRefresh: boolean = false) { this.apiRequestCreator.makeRequest("GET", "users/me", undefined, (err) => { if (err.statusCode) { + this.tokenStorage.ClearStoredUser(); const refreshToken: string | null = this.tokenStorage.GetStoredRefreshToken(); if (!tryingToRefresh && refreshToken !== null) { @@ -183,6 +191,7 @@ export class AuthService { .subscribe((data) => { this.user = data; this.userWatcher.emit(this.user); + this.tokenStorage.SetStoredUser(this.user); if (callback) callback(); }); } @@ -210,6 +219,7 @@ export class AuthService { this.apiRequestCreator.makeRequest("PUT", "logout", {}).subscribe(); this.tokenStorage.ClearStoredGameToken(); + this.tokenStorage.ClearStoredUser(); } public ResetPassword(passwordSha512: string): void { @@ -298,6 +308,7 @@ export class AuthService { this.userWatcher.emit(undefined); this.tokenStorage.ClearStoredGameToken(); + this.tokenStorage.ClearStoredUser(); }); } @@ -308,6 +319,7 @@ export class AuthService { this.user = data; this.userWatcher.emit(data); + this.tokenStorage.SetStoredUser(data); }); } } diff --git a/src/app/api/token-storage.service.ts b/src/app/api/token-storage.service.ts index 45a3e8e..18f2c49 100644 --- a/src/app/api/token-storage.service.ts +++ b/src/app/api/token-storage.service.ts @@ -1,33 +1,50 @@ import {Injectable} from "@angular/core"; +import {ExtendedUser} from "./types/extended-user"; @Injectable({ - providedIn: 'root' + providedIn: 'root' }) export class TokenStorageService { - private readonly GameTokenKey: string = "game_token"; - private readonly RefreshTokenKey: string = "refresh_token"; + private readonly GameTokenKey: string = "game_token"; + private readonly RefreshTokenKey: string = "refresh_token"; + private readonly UserKey: string = "cached_user_data"; - public GetStoredGameToken(): string | null { - return localStorage.getItem(this.GameTokenKey); - } + public GetStoredGameToken(): string | null { + return localStorage.getItem(this.GameTokenKey); + } - public SetStoredGameToken(token: string): void { - localStorage.setItem(this.GameTokenKey, token); - } + public SetStoredGameToken(token: string): void { + localStorage.setItem(this.GameTokenKey, token); + } - public ClearStoredGameToken(): void { - localStorage.removeItem(this.GameTokenKey); - } + public ClearStoredGameToken(): void { + localStorage.removeItem(this.GameTokenKey); + } - public GetStoredRefreshToken(): string | null { - return localStorage.getItem(this.RefreshTokenKey); - } + public GetStoredRefreshToken(): string | null { + return localStorage.getItem(this.RefreshTokenKey); + } - public SetStoredRefreshToken(refreshToken: string): void { - localStorage.setItem(this.RefreshTokenKey, refreshToken); - } + public SetStoredRefreshToken(refreshToken: string): void { + localStorage.setItem(this.RefreshTokenKey, refreshToken); + } - public ClearStoredRefreshToken(): void { - localStorage.removeItem(this.RefreshTokenKey); - } + public ClearStoredRefreshToken(): void { + localStorage.removeItem(this.RefreshTokenKey); + } + + public GetStoredUser(): ExtendedUser | null { + const str = localStorage.getItem(this.UserKey); + if(!str) return null; + + return JSON.parse(str); + } + + public SetStoredUser(user: ExtendedUser): void { + localStorage.setItem(this.UserKey, JSON.stringify(user)); + } + + public ClearStoredUser(): void { + localStorage.removeItem(this.UserKey); + } }