Skip to content

Commit

Permalink
Cache user in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
jvyden committed Nov 5, 2023
1 parent 66b4e02 commit d56eeb5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
12 changes: 12 additions & 0 deletions src/app/api/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ export class AuthService {
this.userWatcher = new EventEmitter<ExtendedUser | undefined>();

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));
Expand Down Expand Up @@ -168,6 +175,7 @@ export class AuthService {
private GetMyUser(callback: Function | null = null, tryingToRefresh: boolean = false) {
this.apiRequestCreator.makeRequest<ExtendedUser>("GET", "users/me", undefined, (err) => {
if (err.statusCode) {
this.tokenStorage.ClearStoredUser();
const refreshToken: string | null = this.tokenStorage.GetStoredRefreshToken();

if (!tryingToRefresh && refreshToken !== null) {
Expand All @@ -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();
});
}
Expand Down Expand Up @@ -210,6 +219,7 @@ export class AuthService {
this.apiRequestCreator.makeRequest("PUT", "logout", {}).subscribe();

this.tokenStorage.ClearStoredGameToken();
this.tokenStorage.ClearStoredUser();
}

public ResetPassword(passwordSha512: string): void {
Expand Down Expand Up @@ -298,6 +308,7 @@ export class AuthService {

this.userWatcher.emit(undefined);
this.tokenStorage.ClearStoredGameToken();
this.tokenStorage.ClearStoredUser();
});
}

Expand All @@ -308,6 +319,7 @@ export class AuthService {

this.user = data;
this.userWatcher.emit(data);
this.tokenStorage.SetStoredUser(data);
});
}
}
59 changes: 38 additions & 21 deletions src/app/api/token-storage.service.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit d56eeb5

Please sign in to comment.