AuthGuard #15
|
@ -1,5 +1,7 @@
|
|||
import { Routes } from '@angular/router';
|
||||
|
||||
import { AuthGuard } from './shared/guards';
|
||||
|
||||
const simpleLayoutRoutes: Routes = [
|
||||
{
|
||||
path: '',
|
||||
|
@ -43,6 +45,7 @@ export const routes: Routes = [
|
|||
import('./layout/main-layout/layout.component').then(
|
||||
(m) => m.LayoutComponent
|
||||
),
|
||||
canActivate: [AuthGuard],
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
import { inject } from '@angular/core';
|
||||
import { Router, CanActivateFn } from '@angular/router';
|
||||
|
||||
import { map, take } from 'rxjs';
|
||||
|
||||
import { AuthService } from '../service';
|
||||
|
||||
export const AuthGuard: CanActivateFn = () => {
|
||||
const authService: AuthService = inject(AuthService);
|
||||
const router: Router = inject(Router);
|
||||
|
||||
return authService.status().pipe(
|
||||
take(1),
|
||||
map((response) => {
|
||||
if (response.success) {
|
||||
return true;
|
||||
} else {
|
||||
return router.createUrlTree(['/welcome']);
|
||||
}
|
||||
})
|
||||
);
|
||||
};
|
|
@ -0,0 +1 @@
|
|||
export * from './auth.guard';
|
|
@ -1,45 +1,69 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { Injectable, WritableSignal, signal } from '@angular/core';
|
||||
import { toObservable } from '@angular/core/rxjs-interop';
|
||||
|
||||
import { BehaviorSubject, Observable } from 'rxjs';
|
||||
import { Observable, of, throwError } from 'rxjs';
|
||||
import { catchError, shareReplay, tap } from 'rxjs/operators';
|
||||
|
||||
import {
|
||||
AuthenticationApiService,
|
||||
SigninResponseDtoApiModel,
|
||||
SuccessDtoApiModel,
|
||||
UserCredentialsDtoApiModel,
|
||||
} from '../../api';
|
||||
import { AuthenticationApiService } from '../../api/api/authentication.api.service';
|
||||
|
||||
type SuccessResponse = {
|
||||
success: boolean;
|
||||
};
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthService {
|
||||
public isAuthenticated$: BehaviorSubject<boolean> =
|
||||
new BehaviorSubject<boolean>(false);
|
||||
public isAuthenticatedSignal: WritableSignal<boolean> = signal(false);
|
||||
public isAuthenticated$: Observable<boolean> = toObservable(
|
||||
this.isAuthenticatedSignal
|
||||
);
|
||||
private statusCheck$: Observable<SuccessDtoApiModel>;
|
||||
|
||||
public constructor(
|
||||
private readonly authenticationApiService: AuthenticationApiService
|
||||
) {}
|
||||
) {
|
||||
this.statusCheck$ = this.initializeStatusCheck();
|
||||
}
|
||||
|
||||
public signup(
|
||||
credentials: UserCredentialsDtoApiModel
|
||||
): Observable<SuccessResponse> {
|
||||
return this.authenticationApiService.authControllerSignup(credentials);
|
||||
): Observable<SuccessDtoApiModel> {
|
||||
return this.authenticationApiService
|
||||
.authControllerSignup(credentials)
|
||||
.pipe(tap(() => this.isAuthenticatedSignal.set(true)));
|
||||
}
|
||||
|
||||
public signin(
|
||||
credentials: UserCredentialsDtoApiModel
|
||||
): Observable<SigninResponseDtoApiModel> {
|
||||
return this.authenticationApiService.authControllerSignin(credentials);
|
||||
return this.authenticationApiService
|
||||
.authControllerSignin(credentials)
|
||||
.pipe(tap(() => this.isAuthenticatedSignal.set(true)));
|
||||
}
|
||||
|
||||
public signout(): Observable<SuccessResponse> {
|
||||
return this.authenticationApiService.authControllerSignout();
|
||||
public signout(): Observable<SuccessDtoApiModel> {
|
||||
return this.authenticationApiService
|
||||
.authControllerSignout()
|
||||
.pipe(tap(() => this.isAuthenticatedSignal.set(false)));
|
||||
}
|
||||
|
||||
public status(): Observable<SuccessResponse> {
|
||||
return this.authenticationApiService.authControllerStatus();
|
||||
public status(): Observable<SuccessDtoApiModel> {
|
||||
if (this.isAuthenticatedSignal()) {
|
||||
return of({ success: true });
|
||||
}
|
||||
return this.statusCheck$;
|
||||
}
|
||||
|
||||
private initializeStatusCheck(): Observable<SuccessDtoApiModel> {
|
||||
return this.authenticationApiService.authControllerStatus().pipe(
|
||||
tap((response) => this.isAuthenticatedSignal.set(response.success)),
|
||||
catchError((error) => {
|
||||
this.isAuthenticatedSignal.set(false);
|
||||
return throwError(() => error);
|
||||
}),
|
||||
shareReplay(1)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue