feat: auth page

This commit is contained in:
2024-07-10 16:35:14 +03:00
parent 6833105604
commit 112e76ab45
10 changed files with 199 additions and 46 deletions

View File

@@ -0,0 +1,34 @@
<div class="flex flex-col m-6">
<div class="mangalib-auth">
<h2>Авторизация в MangaLib</h2>
<ol class="list-decimal flex flex-col gap-4">
<li>
<p class="font-bold">Авторизация через токен</p>
<div class="flex flex-row gap-4">
<input
type="text"
placeholder="Token"
class="outline-none border-md border px-2"
#libSocialToken
/>
<button
type="button"
(click)="setLibSocialToken()"
class="hover:bg-slate-600 bg-slate-400 p-3 rounded-md text-white"
>
Сохранить
</button>
</div>
</li>
<li>
Вход через скрипт TamperMonkey. Если нет кнопки, значит вы не установили скрипт
<a
href="https://test-front.mangalib.me"
class="hover:bg-slate-600 bg-slate-400 p-3 rounded-md text-white tamperMonkey"
>
Вход
</a>
</li>
</ol>
</div>
</div>

View File

@@ -0,0 +1,3 @@
.tamperMonkey {
display: none;
}

View File

@@ -0,0 +1,58 @@
import { CommonModule } from "@angular/common";
import { AfterViewInit, Component, ElementRef, OnDestroy, ViewChild } from "@angular/core";
import { ActivatedRoute, Router, RouterLink } from "@angular/router";
import { Subject, takeUntil } from "rxjs";
import { RulibAuthService } from "../../services/parsers/rulib/rulib.auth.service";
import { EAuthTokenService } from "./enum";
@Component({
selector: "app-auth",
templateUrl: "./auth.component.html",
styleUrls: ["./auth.component.less"],
standalone: true,
imports: [CommonModule, RouterLink],
})
export class AuthComponent implements AfterViewInit, OnDestroy {
private destroy$ = new Subject<void>();
@ViewChild("libSocialToken") libSocialToken: ElementRef<HTMLInputElement> | null = null;
constructor(
private route: ActivatedRoute,
private router: Router,
private rulibAuthService: RulibAuthService,
) {}
private setToken(service: EAuthTokenService, token: string) {
switch (service) {
case EAuthTokenService.RULIB:
this.rulibAuthService.setToken(token);
break;
default:
this.router.navigate(["/", "auth"]);
return;
}
this.router.navigate(["/"]);
}
setLibSocialToken() {
if (this.libSocialToken) {
const token = this.libSocialToken.nativeElement.value;
this.setToken(EAuthTokenService.RULIB, token);
}
}
ngAfterViewInit(): void {
this.route.queryParams.pipe(takeUntil(this.destroy$)).subscribe((params) => {
const { token, service } = params;
if (token && service) {
this.setToken(service as EAuthTokenService, token);
}
});
if (this.libSocialToken) {
this.libSocialToken.nativeElement.value = this.rulibAuthService.getToken();
}
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}

View File

@@ -0,0 +1,3 @@
export enum EAuthTokenService {
RULIB = "rulib",
}