App to prevent access to 'members' route if user not authorized. Authorization is via email & password converted to a JWT token instead of using a backend.
canActivate function uses auth service to see if user authorized.
// uses auth.service to check if user has token in storage. Returns true if there is a token// returns false if user does not have a token and navigates to initial login page.canActivate(route: ActivatedRouteSnapshot): Observable<boolean>{returnthis.auth.user.pipe(take(1),map(user=>{console.log('Can activate: ',user);if(!user){this.alertCtrl.create({header: 'Unauthorized',message: 'You are not allowed to access that page.',buttons: ['OK']}).then(alert=>alert.present());this.router.navigateByUrl('/');returnfalse;}else{returntrue;}}));}
🆒 Features
JWT token generated and stored using Ionic Storage - viewable in the Dev console.
AuthGuard canActivate only true with this token. Token removed upon logging out.
📋 Status & To-do list
Status: Working. Updated may 2021. Tested using ionic server and dummy user credentials to replace 'register' function.