feat: new token saving
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { HttpClient } from "@angular/common/http";
|
||||
import { Injectable, OnDestroy } from "@angular/core";
|
||||
import { Observable, Subject, catchError, map, of, takeUntil } from "rxjs";
|
||||
import { IToken } from "../token.dto";
|
||||
@Injectable({
|
||||
providedIn: "root",
|
||||
})
|
||||
@@ -10,19 +11,21 @@ export class RulibAuthService implements OnDestroy {
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
setToken(token: string): Observable<boolean> {
|
||||
return this.checkToken(token)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.pipe(
|
||||
map((data) => {
|
||||
if (!data) return false;
|
||||
localStorage.setItem("token", token);
|
||||
return true;
|
||||
}),
|
||||
);
|
||||
return this.checkToken(token).pipe(
|
||||
takeUntil(this.destroy$),
|
||||
map((data) => {
|
||||
if (!data) return false;
|
||||
const token_data: IToken = JSON.parse(localStorage.getItem("token") ?? "{}");
|
||||
token_data.rulib_token = token;
|
||||
localStorage.setItem("token", JSON.stringify(token_data));
|
||||
return true;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
getToken(): string {
|
||||
return localStorage.getItem("token") ?? "";
|
||||
const token_data: IToken = JSON.parse(localStorage.getItem("token") ?? "{}");
|
||||
return token_data.rulib_token ?? "";
|
||||
}
|
||||
|
||||
checkToken(token: string): Observable<boolean> {
|
||||
|
||||
3
apps/NwaifuAnime/src/app/services/parsers/token.dto.ts
Normal file
3
apps/NwaifuAnime/src/app/services/parsers/token.dto.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface IToken {
|
||||
rulib_token?: string;
|
||||
}
|
||||
Reference in New Issue
Block a user