Prevent loading view unless authenticated - javascript

I am new to angularJS.
I have a login page. I have made sessions using servlets.
But when user manipulates the url and hits enter then the servlet is getting called asynchronously and backend checks whether session is active or not.
If user is not logged in , then it gives a pop-up saying "You need to login first."
But the problem is that the template corresponding to that url is loaded first and then the pop-up comes.
I want the user to stay on the login page itself and give a popup on the same page even if he/she tries to change url.
Is it possible with angular to call the servlet first and then if "success" then load the view?
Any help would be appreciated.
Thanks in advance.

Related

Detect if user has been redirected from different domain

I'm redirecting my user from e.g. olddomain.com to newdomain.com.
If the user goes directly to newdomain.com I'm just displaying the page, but if the user was redirected from olddomain.com, then after redirecting and displaying page I want to additionally display some modal to user (saying "hey, we just changed our name to newdomain"). Is it possible to detect in JavaScript if the user was redirected so that I can display the message?
I believe it's impossible to do with DNS redirect, but is it possible using load-balancer redirection (that would be ideal) or in JS by changing window.location?

navigating through a page with a login page in between

I am developing a webpage using HTML, CSS & JS. In a section with a list of links, when a user clicks on any of the items in the list I want the user to first have to sign in before they continue on the path of the item they selected and I want to do this without having to duplicate a sign-in page for each stage. I want the link to first go to the login page(login.html) then continue or go back to the path it was on
the problem now is I don't know how exactly I can achieve this without creating multiple copies of the login page
You must persist user state somewhere, either in browser (not safe at all) or on server.
In server: Save information to cookie/session
In browser: Save information to session/local storage
On every protected page visit check if user has been authorized by checking previously set flag. If it's not authorized, then redirect to login page. After successful authorization redirect back to requested protected page.

After logging off, a user should not able to see the pages by pressing back button of the browser

I am using chrome and mozilla browsers working on reactjs.
after logout i am able to redirect to the login page(please see pic(2))
From this login page if i click on back arrow in the browser I am able to access the application.
As per the project requirement once user logout I will be redirecting to login page from login untill login he should not be able to access application by enetring back button.
For on click on logout function do i need to make any thing or from login component renders i want back event should not work.
Please help me thanks in advnace.
Clicking on logout, you are not clearing your store and hence you are still able to view application when clicking back button.
In your root reducer, while combining reducers, you can handle LOGOUT action and return state as undefined.
Goal : As per the project requirement once user logout I will be redirecting to login page from login untill login he should not be able to access application by enetring back button
Assuming you're looking for javascript solution (tags are javascript, react)
In short, you can't disable back button (or changing browser behaviour) even in native javascript see this
My suggestion
Client side way : use pushstate manipulation see this answer
Server side way : when user click logout, redirect it to temporarily page (lets say landing.php). In your landing, redirect again to login page. This way, when user hit backspace / back button he will be redirected to landing page, which then redirect again to login page making him unable to access application. Dirty, but fastest solution i guess. Take a look at my sample specially for you 😀 sample

Popup window for user login if user has not logged in yet

I have a web applicaiton written in ASP.NET MVC 5. There is to be a landing page in .cshtml. When the user clicks on a button on this page, I am to check whether the user has already logged in (by checking the presence of a specific cookie and its valid value). If the user is already logged in, the landing page should be replaced by the target page. If the user is not logged in yet, a popup window is to be displayed for him to enter his login credentials. If he enters correctly, the login popup window should close, and then the landing page is to be replaced by the target page.
As I don't have prior experience with MVC, I need some help on this. When the user clicks on the button on the landing page, how to send the request to the server to check whether he is already logged in, and only replacing the landing page with the target page if the user is already logged in? Also, if the login popup window is showing, and the user enters a correct credentials, how to replace the landing page with target page?
A simple example would be good.
Thanks in advance.
I am going to attempt to answer this from a higher level instead of provide a generic yet more technical example that is likely going to have to be re-coded to fit your needs.
You will need a boolean variable on your model, lets call it loggedIn. This will represent the logged in state of the user.
You want the controller to check the cookies first
If the cookie reads that they are logged in then assign loggedIn to true
If the cookie reads that they are not logged in then assign loggedIn to false
Once you pass your model to your view, you can then use RAZOR to read the loggedIn boolean. If it is true show the content. If it is false show the partial view for your log in.

When user logout from page ... if user click browser back button ,which will redirect to previous URL and display HTML page for few seconds

I am working in angular js, When a user logged out from the browser and click browser back button (or) user Bookmark URL and load after logout.
will load the controller page and check session exists, If not redirect to login page.
which will take 5 seconds time and display UI of the previous page after logout?
Please give me a solution How to solve this issue
sorry if I ask a basic question, please give me a clear explanation friends
I didn't have any friends working on angular js
you have to use authGuard service provided by angular2.
where you can control user to load particular template on the basis of condition which is based on your condition i.e login/logout in your case
you can use their default methods like canActivate and canActivateChild
see here
http://blog.thoughtram.io/angular/2016/07/18/guards-in-angular-2.html
https://angular.io/docs/ts/latest/guide/router.html
Because you have to check your logic/condition on routing or we can say before page load so you have to use Guard of angular2 in routing

Categories