Introduction
In this article, we'll look at how to handle the login process with Session Authentication. We'll verify if the user is logged in or not, and if he is not, then the route will simply be blocked.
Let’s go through the following Angular principles for better understanding.s
Angular Routing module
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
In an Angular program, there are three ways to store data on the client-side.
- Memory
- Session storage
- Local Storage.
We're using localStorage in this case, which saves data in the client browser. It can only be used in a modern browser. When the window is closed or reopened while in session storage, the data is retained; however, when the tab is closed, the data will be lost. It's easy to create a new Angular project using a command. Given is the localStorage syntax which simply helps you to save up to 10MB of data in the browser.
Using the below command, create a new Angular project.
npm install bootstrap
Add the following line to the src/styles.css file after installation:
Build two components, one for login and one for the dashboard.
Create a service called auth.
Make a new user interface for logging in.
Using the command lines below, create a new guard called auth.
The Angular route guard, which can tell the router whether or not to enable navigation to a requested route, is referred to as Angular guard.
ng g component login
ng g component dashboard
ng g service services/auth //Build a service in the Services folder.
ng g interface interfaces/login //In the interfaces folder, build a new interface.
ng g guard guards/auth //Build a new guard in the Guards folder.
In the Login interfaces, add two properties.
Login.ts
Now navigate to guards/Auth.guard.ts. Import the @angular/router package's 'canActivate' interface.
Auth.guard.ts
Import the AuthGuard module into the app.module.ts format.
Then, from the @angular/forms package, import ReactiveFormsModule and FormsModule into this file.
Our app.module.ts file should now look like this.
Now edit the app-routing.modue.ts file to include login and dashboard routes. To verify user authentication, add the ‘canActivate' attribute here. It will redirect to the login page if the user is not allowed.
In the app.component.html file, add the HTML code below.
The path of the URL is stored in routerLink, and routeLinkActive adds the class active to the selected menu. According to the route URL, will determine which View to show.
Import the ILogin interface into the auth.service.ts file.
Within this service class, add a Logout() method.
Now import Router from the @angular/router package into the login.component.ts module. Within this file, import the login interface and the AuthService.
Also, import the @angular/forms package's FormBuilder, FormGroup, Validators.
Inside the function Object() { [native code] }, create three instances: router, service, and formBuilder.
Set default parameters for a new variable modal as the ILogin interface.
Make a loginForm variable with the FormGroup sort. The message is a string, and the returnURL is also a string.
Build a new formGroup within the ngOnInit() method and apply the necessary validation to both properties.
Looking for Trusted AngularJS Development Company ? Your Search ends here.
Add a get method to make it easier to access the form.
Inside this class, add the Login() method.
As a result, login.component.ts would look something like this:
Login.component.html
Dashboard.component.ts
Implement the logout() method inside this.ts format. This page will be inaccessible if the user is not authenticated, and the user will be directed to the login page.
Dashboard.component.html
The logged user's id will be shown within id if the user is registered.
Conclusion
In this blog, we have learned Angular Login with Session Authentication. We can manage user authentication, route access, and the session variable using this method. If the user is not authenticated, we may use this Angular Guard method to prevent requested navigation.
No comments:
Post a Comment