feat: new token saving

This commit is contained in:
2024-07-18 15:41:09 +03:00
parent 4105933098
commit c427c6d0b4
2 changed files with 16 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
import { HttpClient } from "@angular/common/http"; import { HttpClient } from "@angular/common/http";
import { Injectable, OnDestroy } from "@angular/core"; import { Injectable, OnDestroy } from "@angular/core";
import { Observable, Subject, catchError, map, of, takeUntil } from "rxjs"; import { Observable, Subject, catchError, map, of, takeUntil } from "rxjs";
import { IToken } from "../token.dto";
@Injectable({ @Injectable({
providedIn: "root", providedIn: "root",
}) })
@@ -10,19 +11,21 @@ export class RulibAuthService implements OnDestroy {
constructor(private http: HttpClient) {} constructor(private http: HttpClient) {}
setToken(token: string): Observable<boolean> { setToken(token: string): Observable<boolean> {
return this.checkToken(token) return this.checkToken(token).pipe(
.pipe(takeUntil(this.destroy$)) takeUntil(this.destroy$),
.pipe( map((data) => {
map((data) => { if (!data) return false;
if (!data) return false; const token_data: IToken = JSON.parse(localStorage.getItem("token") ?? "{}");
localStorage.setItem("token", token); token_data.rulib_token = token;
return true; localStorage.setItem("token", JSON.stringify(token_data));
}), return true;
); }),
);
} }
getToken(): string { 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> { checkToken(token: string): Observable<boolean> {

View File

@@ -0,0 +1,3 @@
export interface IToken {
rulib_token?: string;
}