From d1e32a94ca624e58e5f10feccf84f6361dd2fd80 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Wed, 10 Jul 2024 19:22:53 +0300 Subject: [PATCH] feat(rulib): token check --- .../app/components/auth/auth.component.html | 12 +++++++ .../src/app/components/auth/auth.component.ts | 11 ++++++ .../parsers/rulib/rulib.auth.service.ts | 34 ++++++++++++++++--- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/apps/NwaifuAnime/src/app/components/auth/auth.component.html b/apps/NwaifuAnime/src/app/components/auth/auth.component.html index 7906acd..c9e8e75 100644 --- a/apps/NwaifuAnime/src/app/components/auth/auth.component.html +++ b/apps/NwaifuAnime/src/app/components/auth/auth.component.html @@ -29,6 +29,18 @@ Вход +
  • +
    + +

    {{ isRuLibAuth ? "Авторизован" : "Не авторизован" }}

    +
    +
  • diff --git a/apps/NwaifuAnime/src/app/components/auth/auth.component.ts b/apps/NwaifuAnime/src/app/components/auth/auth.component.ts index b83cae7..576b461 100644 --- a/apps/NwaifuAnime/src/app/components/auth/auth.component.ts +++ b/apps/NwaifuAnime/src/app/components/auth/auth.component.ts @@ -15,6 +15,7 @@ import { EAuthTokenService } from "./enum"; export class AuthComponent implements AfterViewInit, OnDestroy { private destroy$ = new Subject(); @ViewChild("libSocialToken") libSocialToken: ElementRef | null = null; + isRuLibAuth = false; constructor( private route: ActivatedRoute, private router: Router, @@ -40,6 +41,15 @@ export class AuthComponent implements AfterViewInit, OnDestroy { } } + checkToken() { + this.rulibAuthService + .checkToken(this.rulibAuthService.getToken()) + .pipe(takeUntil(this.destroy$)) + .subscribe((data) => { + this.isRuLibAuth = data; + }); + } + ngAfterViewInit(): void { this.route.queryParams.pipe(takeUntil(this.destroy$)).subscribe((params) => { const { token, service } = params; @@ -50,6 +60,7 @@ export class AuthComponent implements AfterViewInit, OnDestroy { if (this.libSocialToken) { this.libSocialToken.nativeElement.value = this.rulibAuthService.getToken(); } + this.checkToken(); } ngOnDestroy(): void { this.destroy$.next(); diff --git a/apps/NwaifuAnime/src/app/services/parsers/rulib/rulib.auth.service.ts b/apps/NwaifuAnime/src/app/services/parsers/rulib/rulib.auth.service.ts index 0708796..d1d2730 100644 --- a/apps/NwaifuAnime/src/app/services/parsers/rulib/rulib.auth.service.ts +++ b/apps/NwaifuAnime/src/app/services/parsers/rulib/rulib.auth.service.ts @@ -1,15 +1,41 @@ -import { Injectable } from "@angular/core"; +import { HttpClient } from "@angular/common/http"; +import { Injectable, OnDestroy } from "@angular/core"; +import { Observable, Subject, catchError, map, of, takeUntil } from "rxjs"; @Injectable({ providedIn: "root", }) -export class RulibAuthService { +export class RulibAuthService implements OnDestroy { + private destroy$ = new Subject(); + private api_url = "https://api.lib.social"; + constructor(private http: HttpClient) {} + setToken(token: string) { - localStorage.setItem("token", token); + this.checkToken(token) + .pipe(takeUntil(this.destroy$)) + .subscribe((data) => { + if (!data) return; + localStorage.setItem("token", token); + }); } getToken(): string { return localStorage.getItem("token") ?? ""; } - //TODO: Проверка токена + checkToken(token: string): Observable { + return this.http + .get(this.api_url + "/api/auth/me", { + headers: { Authorization: `Bearer ${token}` }, + }) + .pipe( + takeUntil(this.destroy$), + map(() => true), + catchError(() => of(false)), + ); + } + + ngOnDestroy(): void { + this.destroy$.next(); + this.destroy$.complete(); + } }