I am developing an Angular application with Spring boot. I have developed a login page that takes username and password as input. A web service which authenticates the user from the backend and sends a response with success and failure. And a homepage that gets displayed after successful authentication.
What's happening is that I can also open the home page URL directly without login and that's not the correct one.
What I need is to implement a mechanism that will checks if the user is logged in or not and based on that it would display a URL page or a login page.
One of the best practices to handle authentication in web applications is using JWT to generate and check Token on the server side and passing token in the communications of Front-end & back-and. When you implement this and after authentication in server, you have to store Token in LocalStorage or SessionStorage or cockies for future interactions.
For creating protected routes in Angular application you have to implement these:
Here is a sample code for you:
app-AuthGuard.ts
auth.service.ts
app.routing.module.ts
For more information and complete example read this article: Angular 8 - Role Based Authorization Tutorial with Example