February 2019
Intermediate to advanced
446 pages
10h 55m
English
Now, we can modify the Auth Guard to add the condition, which will return true or false, based on the logged-in user. Modified code is marked with bold text. The state URL property contains the page that redirects to the Login page. After successful login, state.url is used to navigate back to the same page:
import { Observable } from 'rxjs';@Injectable({ providedIn: 'root'})export class AuthGuard implements CanActivate { constructor(private router: Router) { } canActivate( next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { if (localStorage.getItem('currentUser')) { return true; } // redirect to login page with the return url // when user is not logged-in ...Read now
Unlock full access