feat(rulib): token check

This commit is contained in:
2024-07-10 19:22:53 +03:00
parent 82a0ba102d
commit d1e32a94ca
3 changed files with 53 additions and 4 deletions

View File

@@ -29,6 +29,18 @@
Вход Вход
</a> </a>
</li> </li>
<li>
<div class="flex flex-row gap-3">
<button
type="button"
(click)="checkToken()"
class="hover:bg-slate-600 bg-slate-400 p-3 rounded-md text-white"
>
Проверить токен
</button>
<h3 class="self-center">{{ isRuLibAuth ? "Авторизован" : "Не авторизован" }}</h3>
</div>
</li>
</ol> </ol>
</div> </div>
</div> </div>

View File

@@ -15,6 +15,7 @@ import { EAuthTokenService } from "./enum";
export class AuthComponent implements AfterViewInit, OnDestroy { export class AuthComponent implements AfterViewInit, OnDestroy {
private destroy$ = new Subject<void>(); private destroy$ = new Subject<void>();
@ViewChild("libSocialToken") libSocialToken: ElementRef<HTMLInputElement> | null = null; @ViewChild("libSocialToken") libSocialToken: ElementRef<HTMLInputElement> | null = null;
isRuLibAuth = false;
constructor( constructor(
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, 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 { ngAfterViewInit(): void {
this.route.queryParams.pipe(takeUntil(this.destroy$)).subscribe((params) => { this.route.queryParams.pipe(takeUntil(this.destroy$)).subscribe((params) => {
const { token, service } = params; const { token, service } = params;
@@ -50,6 +60,7 @@ export class AuthComponent implements AfterViewInit, OnDestroy {
if (this.libSocialToken) { if (this.libSocialToken) {
this.libSocialToken.nativeElement.value = this.rulibAuthService.getToken(); this.libSocialToken.nativeElement.value = this.rulibAuthService.getToken();
} }
this.checkToken();
} }
ngOnDestroy(): void { ngOnDestroy(): void {
this.destroy$.next(); this.destroy$.next();

View File

@@ -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({ @Injectable({
providedIn: "root", providedIn: "root",
}) })
export class RulibAuthService { export class RulibAuthService implements OnDestroy {
private destroy$ = new Subject<void>();
private api_url = "https://api.lib.social";
constructor(private http: HttpClient) {}
setToken(token: string) { 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 { getToken(): string {
return localStorage.getItem("token") ?? ""; return localStorage.getItem("token") ?? "";
} }
//TODO: Проверка токена checkToken(token: string): Observable<boolean> {
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();
}
} }