I have successfully implement AuthGuardService which restrict access to a protected route if user is not logged in.
What I am trying to achieve is, if user is already logged in and accessed the Login Route, I want it to redirect to another route like homepage.
Angular is completely independent of the server. We are using stateless REST API to get data. So use AuthGuard to protect the routes from unauthorized access.
AuthGuard is used to protect the routes from unauthorized access in angular. How AuthGuard Works? Auth guard provide lifecycle event called canActivate. The canActivate is like a constructor. It will be called before accessing the routes.
How do I restrict the url for user in Angularjs? You can use ‘Route Guard’ for this. Route Guards are basically service (as they are injectable), where you define various cases in which a route must be hit. Like, for example, if there are two types of user -> Admin and normal user.
Manage the user authentication permission for the Angular path. Angular Auth Guard This Angular function helps a lot when it comes to handling authentication. This is an interface that instructs the router whether or not to enable navigation to a specific route. We're using the 'canActivate' guard form in this case. Angular localStorage
With Angular, we can implement this flow using route guards and the router to help manage redirects. Imagine we’re building a forum, and we want to ensure that a user is logged in before they can post a new message.
logout () function removes the user's information about login from local storage. isLoggedIn () function inform us whether the user is logged into the system. getRole () function gives us the user's role from local storage. To create a guard you should use angular-cli command. This can be like below.
You can create two CanActivate guard:
- For restricting routes for already loggedIn users (eg: /login
, /register
etc)
- For restricting routes for not loggedIn users (eg: /dashboard
)
loggedIn() {
//return boolean for loggedIn user logic
}
import { Injectable } from "@angular/core";
import { CanActivate, Router } from "@angular/router";
import { AuthService } from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private _authService: AuthService, private _router: Router) { }
canActivate(): boolean {
if (this._authService.loggedIn()) {
return true;
} else {
this._router.navigate(['/login'])
return false
}
}
}
import { Injectable } from "@angular/core";
import { CanActivate, Router } from "@angular/router";
import { AuthService } from './auth.service';
@Injectable()
export class LoggedInAuthGuard implements CanActivate {
constructor(private _authService: AuthService, private _router: Router) { }
canActivate(): boolean {
if (this._authService.loggedIn()) {
this._router.navigate(['/dashboard'])
return false
} else {
return true
}
}
}
...
providers:[AuthGuard,LoggedInAuthGuard]
...
const routes: Route[] = [
{ path: "/login", component: LoginComponent, canActivate:[LoggedInAuthGuard] },
{ path: "/dashboard, component: DashboardComponent, canActivate: [AuthGuard]}
]
You could perform a simple check in the ngOnInit of the login component like so and redirect to another page of your choice if they are authenticated already:
ngOnInit() {
if (this._authService.isLoggedIn) {
this._router.navigate(['/apps']);
}
}
That worked for me!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With