Compare commits

..

No commits in common. "d587c860c49d90343a9ee53b586a4eeb4733fcd7" and "3362115ba6e78bb69f663ce4d651ec85658a8bf9" have entirely different histories.

4 changed files with 17 additions and 67 deletions

View File

@ -1,7 +1,5 @@
import { Routes } from '@angular/router'; import { Routes } from '@angular/router';
import { AuthGuard } from './shared/guards';
const simpleLayoutRoutes: Routes = [ const simpleLayoutRoutes: Routes = [
{ {
path: '', path: '',
@ -45,7 +43,6 @@ export const routes: Routes = [
import('./layout/main-layout/layout.component').then( import('./layout/main-layout/layout.component').then(
(m) => m.LayoutComponent (m) => m.LayoutComponent
), ),
canActivate: [AuthGuard],
children: [ children: [
{ {
path: '', path: '',

View File

@ -1,22 +0,0 @@
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']);
}
})
);
};

View File

@ -1 +0,0 @@
export * from './auth.guard';

View File

@ -1,69 +1,45 @@
import { Injectable, WritableSignal, signal } from '@angular/core'; import { Injectable } from '@angular/core';
import { toObservable } from '@angular/core/rxjs-interop';
import { Observable, of, throwError } from 'rxjs'; import { BehaviorSubject, Observable } from 'rxjs';
import { catchError, shareReplay, tap } from 'rxjs/operators';
import { import {
AuthenticationApiService,
SigninResponseDtoApiModel, SigninResponseDtoApiModel,
SuccessDtoApiModel,
UserCredentialsDtoApiModel, UserCredentialsDtoApiModel,
} from '../../api'; } from '../../api';
import { AuthenticationApiService } from '../../api/api/authentication.api.service';
type SuccessResponse = {
success: boolean;
};
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class AuthService { export class AuthService {
public isAuthenticatedSignal: WritableSignal<boolean> = signal(false); public isAuthenticated$: BehaviorSubject<boolean> =
public isAuthenticated$: Observable<boolean> = toObservable( new BehaviorSubject<boolean>(false);
this.isAuthenticatedSignal
);
private statusCheck$: Observable<SuccessDtoApiModel>;
public constructor( public constructor(
private readonly authenticationApiService: AuthenticationApiService private readonly authenticationApiService: AuthenticationApiService
) { ) {}
this.statusCheck$ = this.initializeStatusCheck();
}
public signup( public signup(
credentials: UserCredentialsDtoApiModel credentials: UserCredentialsDtoApiModel
): Observable<SuccessDtoApiModel> { ): Observable<SuccessResponse> {
return this.authenticationApiService return this.authenticationApiService.authControllerSignup(credentials);
.authControllerSignup(credentials)
.pipe(tap(() => this.isAuthenticatedSignal.set(true)));
} }
public signin( public signin(
credentials: UserCredentialsDtoApiModel credentials: UserCredentialsDtoApiModel
): Observable<SigninResponseDtoApiModel> { ): Observable<SigninResponseDtoApiModel> {
return this.authenticationApiService return this.authenticationApiService.authControllerSignin(credentials);
.authControllerSignin(credentials)
.pipe(tap(() => this.isAuthenticatedSignal.set(true)));
} }
public signout(): Observable<SuccessDtoApiModel> { public signout(): Observable<SuccessResponse> {
return this.authenticationApiService return this.authenticationApiService.authControllerSignout();
.authControllerSignout()
.pipe(tap(() => this.isAuthenticatedSignal.set(false)));
} }
public status(): Observable<SuccessDtoApiModel> { public status(): Observable<SuccessResponse> {
if (this.isAuthenticatedSignal()) { return this.authenticationApiService.authControllerStatus();
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)
);
} }
} }