Created
December 20, 2017 01:21
-
-
Save LibertyBeta/991f9ef3b0c14690918e5d8f3743536f to your computer and use it in GitHub Desktop.
Fitler Guard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { CanActivate, CanActivateChild, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router'; | |
import { Observable } from 'rxjs/Observable'; | |
import { AutService } from '../services/auth.service'; | |
import { nearer } from './/Users/rainer/Documents/Checkmate/Staunch-Odigos/node_modules/@types/q'; | |
@Injectable() | |
export class ComplexGuard implements CanActivate, CanActivateChild { | |
constructor(private _as: AutService, private _router: Router) { } | |
canActivate( | |
next: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { | |
// If the user is logged in, we don't need to filter out our token, so leave them be. | |
if(this._as.isLogged()){ | |
return true; | |
} else { | |
// check to see if the auth query is there. | |
if(next.params['auth']){ | |
const authCheck = this._as.checktoken(next.params['auth']); | |
authCheck.subscribe((cleared=>{ | |
if(cleared === true){ | |
const extras = { | |
params: next.params, | |
outet: next.outlet, | |
queryParams: next.queryParams, | |
fragment: next.fragment, | |
data: next.data | |
}; | |
delete extras.params['auth']; | |
const paths = next.pathFromRoot(); | |
let acc=''; | |
for(const path of paths){ | |
if(path.url.length !== 0){ | |
acc += `/${path.url[0].path}`; | |
} | |
} | |
this._router.navigateByUrl(acc, extras); | |
} | |
})); | |
return authCheck; | |
} else { | |
//No auth token? Get out of here | |
this._router.navigate(['/', '503']); | |
return false; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment