Reverted back to "canActivate" & "canActivateChild"

Converted guards to functional guards
This commit is contained in:
Sercan Yemen 2023-01-22 22:49:25 +03:00
parent 1e8c284c93
commit 715ab6a3aa
4 changed files with 46 additions and 113 deletions

View File

@ -22,7 +22,8 @@ export const appRoutes: Route[] = [
// Auth routes for guests
{
path: '',
canMatch: [NoAuthGuard],
canActivate: [NoAuthGuard],
canActivateChild: [NoAuthGuard],
component: LayoutComponent,
data: {
layout: 'empty'
@ -39,7 +40,8 @@ export const appRoutes: Route[] = [
// Auth routes for authenticated users
{
path: '',
canMatch: [AuthGuard],
canActivate: [AuthGuard],
canActivateChild: [AuthGuard],
component: LayoutComponent,
data: {
layout: 'empty'
@ -65,7 +67,8 @@ export const appRoutes: Route[] = [
// Admin routes
{
path: '',
canMatch: [AuthGuard],
canActivate: [AuthGuard],
canActivateChild: [AuthGuard],
component: LayoutComponent,
resolve: {
initialData: InitialDataResolver,

View File

@ -189,7 +189,7 @@ export class AuthService
return of(false);
}
// If the access token exists and it didn't expire, sign in using it
// If the access token exists, and it didn't expire, sign in using it
return this.signInUsingToken();
}
}

View File

@ -1,60 +1,21 @@
import { Injectable } from '@angular/core';
import { CanMatch, Route, Router, UrlSegment, UrlTree } from '@angular/router';
import { Observable, of, switchMap } from 'rxjs';
import { inject } from '@angular/core';
import { CanActivateChildFn, CanActivateFn, Router } from '@angular/router';
import { of, switchMap } from 'rxjs';
import { AuthService } from 'app/core/auth/auth.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanMatch
{
/**
* Constructor
*/
constructor(
private _authService: AuthService,
private _router: Router
)
{
}
export const AuthGuard: CanActivateFn | CanActivateChildFn = (route, state) => {
const router: Router = inject(Router);
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
/**
* Can match
*
* @param route
* @param segments
*/
canMatch(route: Route, segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
{
return this._check(segments);
}
// -----------------------------------------------------------------------------------------------------
// @ Private methods
// -----------------------------------------------------------------------------------------------------
/**
* Check the authenticated status
*
* @param segments
* @private
*/
private _check(segments: UrlSegment[]): Observable<boolean | UrlTree>
{
// Check the authentication status
return this._authService.check().pipe(
return inject(AuthService).check().pipe(
switchMap((authenticated) => {
// If the user is not authenticated...
if ( !authenticated )
{
// Redirect to the sign-in page with a redirectUrl param
const redirectURL = `/${segments.join('/')}`;
const urlTree = this._router.parseUrl(`sign-in?redirectURL=${redirectURL}`);
const redirectURL = state.url === '/sign-out' ? '' : `redirectURL=${state.url}`;
const urlTree = router.parseUrl(`sign-in?${redirectURL}`);
return of(urlTree);
}
@ -63,5 +24,4 @@ export class AuthGuard implements CanMatch
return of(true);
})
);
}
}
};

View File

@ -1,53 +1,23 @@
import { Injectable } from '@angular/core';
import { CanMatch, Route, Router, UrlSegment, UrlTree } from '@angular/router';
import { Observable, of, switchMap } from 'rxjs';
import { inject } from '@angular/core';
import { CanActivateChildFn, CanActivateFn, Router } from '@angular/router';
import { of, switchMap } from 'rxjs';
import { AuthService } from 'app/core/auth/auth.service';
@Injectable({
providedIn: 'root'
export const NoAuthGuard: CanActivateFn | CanActivateChildFn = (route, state) => {
const router: Router = inject(Router);
// Check the authentication status
return inject(AuthService).check().pipe(
switchMap((authenticated) => {
// If the user is authenticated...
if ( authenticated )
{
return of(router.parseUrl(''));
}
// Allow the access
return of(true);
})
export class NoAuthGuard implements CanMatch
{
/**
* Constructor
*/
constructor(
private _authService: AuthService,
private _router: Router
)
{
}
// -----------------------------------------------------------------------------------------------------
// @ Public methods
// -----------------------------------------------------------------------------------------------------
/**
* Can match
*
* @param route
* @param segments
*/
canMatch(route: Route, segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
{
return this._check();
}
// -----------------------------------------------------------------------------------------------------
// @ Private methods
// -----------------------------------------------------------------------------------------------------
/**
* Check the authenticated status
*
* @private
*/
private _check(): Observable<boolean>
{
// Check the authentication status and return an observable of
// "true" or "false" to allow or prevent the access
return this._authService.check().pipe(
switchMap((authenticated) => of(!authenticated))
);
}
}
};