92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { inject, Injectable } from '@angular/core';
|
|
import { catchError, map, Observable, of, tap } from 'rxjs';
|
|
import { LoginResponse, RegisterResponse } from './responses/auth';
|
|
import { ActivatedRouteSnapshot, CanActivateFn, Router, RouterStateSnapshot } from '@angular/router';
|
|
import { environment } from '../../environment/environment';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class AuthService {
|
|
private readonly SESSION_COOKIE: string = "session";
|
|
private readonly USERNAME_FIELD: string = "username";
|
|
private readonly PASSWORD_FIELD: string = "password";
|
|
private readonly REPEAT_PASSWORD_FIELD: string = "repeatPassword";
|
|
|
|
private loggedIn: boolean = false;
|
|
|
|
constructor(private http: HttpClient) { }
|
|
|
|
public Login(username: string, password: string): Observable<LoginResponse> {
|
|
let url = `${environment.apiBase}/auth/login`;
|
|
|
|
let formData = new FormData();
|
|
formData.append(this.USERNAME_FIELD, username);
|
|
formData.append(this.PASSWORD_FIELD, password);
|
|
|
|
return this.http.post<LoginResponse>(url, formData, { withCredentials: true })
|
|
}
|
|
|
|
public Register(username: string, password: string, repeatPassword: string): Observable<RegisterResponse> {
|
|
let url = `${environment.apiBase}/auth/register`;
|
|
|
|
let formData = new FormData();
|
|
formData.append(this.USERNAME_FIELD, username);
|
|
formData.append(this.PASSWORD_FIELD, password);
|
|
formData.append(this.REPEAT_PASSWORD_FIELD, repeatPassword);
|
|
|
|
return this.http.post<RegisterResponse>(url, formData, { withCredentials: true })
|
|
}
|
|
|
|
public Logout(): Observable<boolean> {
|
|
let url = `${environment.apiBase}/auth/logout`;
|
|
|
|
return this.http.get(url, { withCredentials: true }).pipe(
|
|
map(() => true),
|
|
catchError(() => of(false))
|
|
);
|
|
}
|
|
|
|
public Bump(): Observable<boolean> {
|
|
let url = `${environment.apiBase}/auth/bump`
|
|
|
|
return this.http.get(url, { withCredentials: true }).pipe(
|
|
map(() => true),
|
|
catchError(() => of(false)),
|
|
tap(result => this.loggedIn = result)
|
|
);
|
|
}
|
|
|
|
public HasToken(): boolean {
|
|
let cookies = document.cookie.split(';');
|
|
let found = false;
|
|
cookies.forEach(cookie => {
|
|
cookie = cookie.trim();
|
|
found = found || cookie.startsWith(this.SESSION_COOKIE + "=") && cookie.split('=', 2)[1] != '';
|
|
});
|
|
|
|
return found;
|
|
}
|
|
|
|
public IsLoggedIn(): boolean { return this.loggedIn; }
|
|
}
|
|
|
|
export const IsLoggedInCanActivate: CanActivateFn = (
|
|
_: ActivatedRouteSnapshot,
|
|
__: RouterStateSnapshot
|
|
): Observable<boolean> => {
|
|
const authService = inject(AuthService);
|
|
const router = inject(Router);
|
|
|
|
return authService.Bump().pipe(
|
|
map(isValid => {
|
|
if (isValid) {
|
|
return true;
|
|
} else {
|
|
router.navigate(['auth/login']);
|
|
return false;
|
|
}
|
|
})
|
|
);
|
|
}; |