mirror of
https://github.com/richard-loafle/fuse-angular.git
synced 2024-10-30 01:08:47 +00:00
Reverted back to "canActivate" & "canActivateChild"
Converted guards to functional guards
This commit is contained in:
parent
1e8c284c93
commit
715ab6a3aa
|
@ -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,
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,67 +1,27 @@
|
|||
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
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// Check the authentication status
|
||||
return inject(AuthService).check().pipe(
|
||||
switchMap((authenticated) => {
|
||||
|
||||
/**
|
||||
* Can match
|
||||
*
|
||||
* @param route
|
||||
* @param segments
|
||||
*/
|
||||
canMatch(route: Route, segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
|
||||
{
|
||||
return this._check(segments);
|
||||
}
|
||||
// If the user is not authenticated...
|
||||
if ( !authenticated )
|
||||
{
|
||||
// Redirect to the sign-in page with a redirectUrl param
|
||||
const redirectURL = state.url === '/sign-out' ? '' : `redirectURL=${state.url}`;
|
||||
const urlTree = router.parseUrl(`sign-in?${redirectURL}`);
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// @ Private methods
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
return of(urlTree);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the authenticated status
|
||||
*
|
||||
* @param segments
|
||||
* @private
|
||||
*/
|
||||
private _check(segments: UrlSegment[]): Observable<boolean | UrlTree>
|
||||
{
|
||||
// Check the authentication status
|
||||
return this._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}`);
|
||||
|
||||
return of(urlTree);
|
||||
}
|
||||
|
||||
// Allow the access
|
||||
return of(true);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
// Allow the access
|
||||
return of(true);
|
||||
})
|
||||
);
|
||||
};
|
||||
|
|
|
@ -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 class NoAuthGuard implements CanMatch
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
constructor(
|
||||
private _authService: AuthService,
|
||||
private _router: Router
|
||||
)
|
||||
{
|
||||
}
|
||||
export const NoAuthGuard: CanActivateFn | CanActivateChildFn = (route, state) => {
|
||||
const router: Router = inject(Router);
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// @ Public methods
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// Check the authentication status
|
||||
return inject(AuthService).check().pipe(
|
||||
switchMap((authenticated) => {
|
||||
|
||||
/**
|
||||
* Can match
|
||||
*
|
||||
* @param route
|
||||
* @param segments
|
||||
*/
|
||||
canMatch(route: Route, segments: UrlSegment[]): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
|
||||
{
|
||||
return this._check();
|
||||
}
|
||||
// If the user is authenticated...
|
||||
if ( authenticated )
|
||||
{
|
||||
return of(router.parseUrl(''));
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------
|
||||
// @ 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))
|
||||
);
|
||||
}
|
||||
}
|
||||
// Allow the access
|
||||
return of(true);
|
||||
})
|
||||
);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user