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 { AuthGuard } from './shared/guards';
const simpleLayoutRoutes: Routes = [
{
path: '',
@ -45,7 +43,6 @@ export const routes: Routes = [
import('./layout/main-layout/layout.component').then(
(m) => m.LayoutComponent
),
canActivate: [AuthGuard],
children: [
{
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 { toObservable } from '@angular/core/rxjs-interop';
import { Injectable } from '@angular/core';
import { Observable, of, throwError } from 'rxjs';
import { catchError, shareReplay, tap } from 'rxjs/operators';
import { BehaviorSubject, Observable } from 'rxjs';
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 isAuthenticatedSignal: WritableSignal<boolean> = signal(false);
public isAuthenticated$: Observable<boolean> = toObservable(
this.isAuthenticatedSignal
);
private statusCheck$: Observable<SuccessDtoApiModel>;
public isAuthenticated$: BehaviorSubject<boolean> =
new BehaviorSubject<boolean>(false);
public constructor(
private readonly authenticationApiService: AuthenticationApiService
) {
this.statusCheck$ = this.initializeStatusCheck();
}
) {}
public signup(
credentials: UserCredentialsDtoApiModel
): Observable<SuccessDtoApiModel> {
return this.authenticationApiService
.authControllerSignup(credentials)
.pipe(tap(() => this.isAuthenticatedSignal.set(true)));
): Observable<SuccessResponse> {
return this.authenticationApiService.authControllerSignup(credentials);
}
public signin(
credentials: UserCredentialsDtoApiModel
): Observable<SigninResponseDtoApiModel> {
return this.authenticationApiService
.authControllerSignin(credentials)
.pipe(tap(() => this.isAuthenticatedSignal.set(true)));
return this.authenticationApiService.authControllerSignin(credentials);
}
public signout(): Observable<SuccessDtoApiModel> {
return this.authenticationApiService
.authControllerSignout()
.pipe(tap(() => this.isAuthenticatedSignal.set(false)));
public signout(): Observable<SuccessResponse> {
return this.authenticationApiService.authControllerSignout();
}
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)
);
public status(): Observable<SuccessResponse> {
return this.authenticationApiService.authControllerStatus();
}
}