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 { 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> {

View File

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