2024-07-01 16:21:47 +09:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Router } from '@angular/router';
|
2024-07-02 10:37:40 +09:00
|
|
|
import { catchError, map, Observable, of, ReplaySubject, switchMap, tap } from 'rxjs';
|
2024-07-01 16:21:47 +09:00
|
|
|
import { ServicesApiServices } from './services-api.service';
|
|
|
|
|
|
|
|
export interface IAuth {
|
|
|
|
token: string;
|
|
|
|
user: {
|
|
|
|
userId: number;
|
|
|
|
username: string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class AuthServiceMempool {
|
|
|
|
private auth$: ReplaySubject<IAuth | null> = new ReplaySubject(1);
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private servicesApiService: ServicesApiServices,
|
|
|
|
private router: Router,
|
|
|
|
) {
|
|
|
|
const localStorageAuth = localStorage.getItem('auth');
|
|
|
|
if (!localStorageAuth || localStorageAuth.length === 0) {
|
|
|
|
this.setAuth(null);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
this.setAuth(JSON.parse(localStorageAuth));
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Unable to parse 'auth' from localStorage`, e);
|
|
|
|
localStorage.removeItem('auth');
|
|
|
|
this.setAuth(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshAuth$(): Observable<IAuth | null> {
|
|
|
|
return this.servicesApiService.getJWT$()
|
|
|
|
.pipe(
|
|
|
|
tap((user) => {
|
|
|
|
this.setAuth(user);
|
|
|
|
}),
|
|
|
|
map((user) => {
|
|
|
|
return user;
|
2024-07-02 10:37:40 +09:00
|
|
|
}),
|
|
|
|
catchError(() => of(null)),
|
2024-07-01 16:21:47 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
logout() {
|
|
|
|
this.setAuth(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
setAuth(auth: any) {
|
|
|
|
if (!auth) {
|
|
|
|
localStorage.removeItem('auth');
|
|
|
|
} else {
|
|
|
|
localStorage.setItem('auth', JSON.stringify(auth));
|
|
|
|
}
|
|
|
|
this.auth$.next(auth);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAuth$(): Observable<IAuth | null> {
|
|
|
|
if (!localStorage.getItem('auth')) {
|
|
|
|
return this.refreshAuth$().pipe(
|
|
|
|
switchMap(() => this.auth$.asObservable())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return this.auth$.asObservable();
|
|
|
|
}
|
|
|
|
}
|