-
-
Save lasfrancisco/494c0635d5c184151e255742a0f63238 to your computer and use it in GitHub Desktop.
This file contains 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
@Injectable() | |
export class ActionLinkObserver implements CanActivate { | |
private observers$ = new Subject<ActivatedRouteSnapshot>(); | |
// Implements single route user authentication guarding | |
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | |
// Pushes the snapshot for observers to react, eventually | |
this.observers$.next(route); | |
// Always prevents the real routing | |
return false; | |
} | |
/** Turns a ParamMap into an object */ | |
private extract(params: ParamMap): { [key: string]: string } | undefined { | |
// Skips when no params are present | |
if(!params || params.keys.length <= 0) { return undefined; } | |
// Reduces the keys arrayn into the resulting object | |
return params.keys.reduce( (obj, key) => { | |
// Adds the single key, value pair | |
obj[key] = params.get(key); | |
return obj; | |
}, {}); | |
} | |
/** Register the observer returning the observable emitting on the specified action(s) */ | |
public register(...actions: string[]): Observable<{ [key: string]: string } | undefined> { | |
return this.observers$.pipe( | |
// Filters the request based on the action code | |
filter( request => actions.some( action => request.routerConfig.path === action ) ), | |
// Emits the route's quesy parameters' map as an object | |
map( route => this.extract(route.queryParamMap) ) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment