Fix: Navigating Booklore on a new device requires refreshes

This commit is contained in:
aditya.chandel
2025-08-10 01:33:18 -06:00
committed by Aditya Chandel
parent b1508493cf
commit ea84a9dc6a
6 changed files with 50 additions and 42 deletions
@@ -1,11 +1,10 @@
import {inject, Injectable, Injector} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable, tap} from 'rxjs';
import {BehaviorSubject, Observable, tap} from 'rxjs';
import {RxStompService} from '../../shared/websocket/rx-stomp.service';
import {API_CONFIG} from '../../config/api-config';
import {createRxStompConfig} from '../../shared/websocket/rx-stomp.config';
import {OAuthService} from 'angular-oauth2-oidc';
import {AppSettingsService} from './app-settings.service';
import {Router} from '@angular/router';
@Injectable({
@@ -21,6 +20,9 @@ export class AuthService {
private oAuthService = inject(OAuthService);
private router = inject(Router);
private tokenSubject = new BehaviorSubject<string | null>(this.getOidcAccessToken() || this.getInternalAccessToken());
public token$ = this.tokenSubject.asObservable();
internalLogin(credentials: { username: string; password: string }): Observable<{ accessToken: string; refreshToken: string, isDefaultPassword: string }> {
return this.http.post<{ accessToken: string; refreshToken: string, isDefaultPassword: string }>(`${this.apiUrl}/login`, credentials).pipe(
tap((response) => {
@@ -57,6 +59,7 @@ export class AuthService {
saveInternalTokens(accessToken: string, refreshToken: string): void {
localStorage.setItem('accessToken_Internal', accessToken);
localStorage.setItem('refreshToken_Internal', refreshToken);
this.tokenSubject.next(accessToken);
}
getInternalAccessToken(): string | null {
@@ -74,6 +77,7 @@ export class AuthService {
logout(): void {
localStorage.removeItem('accessToken_Internal');
localStorage.removeItem('refreshToken_Internal');
this.tokenSubject.next(null);
this.getRxStompService().deactivate();
if (this.oAuthService.clientId) {
this.oAuthService.logOut();
@@ -0,0 +1,26 @@
import { Injectable, inject } from '@angular/core';
import { AuthService } from './auth.service';
import { UserService } from '../../settings/user-management/user.service';
import { filter, catchError } from 'rxjs/operators';
import { of } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class StartupService {
private auth = inject(AuthService);
private userSvc = inject(UserService);
load(): Promise<void> {
this.auth.token$
.pipe(filter(t => !!t))
.subscribe(() => {
this.userSvc.getMyself()
.pipe(catchError(() => of(null)))
.subscribe(user => {
if (user) {
this.userSvc.setInitialUser(user);
}
});
});
return Promise.resolve();
}
}