Closed nigel-dewar closed 8 years ago
You're right This example requires a fair bit of refactoring, I will probably do it when Angular2 reaches it's first release. Meanwhile I update it so it is up-to-date with the latest release with the minimum changes necessary, any pull requests will be welcome.
anyway if someone searches for an example of an AuthGuard and gets here this is a simple one I use:
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private auth: Auth, private router: Router) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
) {
if (this.auth.isAuthenticated()) { return true; }
if (state.url === <default main path>) {
this.router.navigate([<login path>])
} else {
this.router.navigate([<login path>, {url: encodeURIComponent(state.url) }]); //don't know if the encodeURIComponent is still necessary in RC5, it was in RC4
}
return false;
}
}
As for the refresh token one you can probably implement it in the JwtHttp class (check expiration before call and try to refresh), should be simple enough. You'll have to add the refresh path to the config and probably handle the 401 errors yourself to navigate to the login screen since otherwise I would be coupled with the router and there are people that use different routers. You could add this as an issue in the ng2-ui-auth library I will probably get to it when I have more time.
Cool thanks Ron for the update, and code you use for AuthGuard
I will have a crack at implementing refresh token on the JwtHttp class this weekend.
Thanks very much Ron.
Nigel.
Hi Ron,
I have ng2-ui-auth working with Angular2 cli, using RC5. It works great.
working through I see a couple of areas that could enhance your example app, these are just suggestions.
If your super busy, and I assume you are, I am nearly finished implementing this on my repo and when I am done I can share what I have done with you as example. Although I am sure you will come up with much better way, but none the less no harm in sharing right.
cheers, Feel free to close this when you get it as its not an issue. I dont know how to mark these things up as questions or suggestions in github.
Nigel