Lana Hanna
Lana Hanna

Reputation: 111

how to prevent user from access to main page if he wasn't logged in?

I am using js vanilla with html and pure php
I made sign up and login pages with database. when user logged in login page successfully, it will execute

sessionStorage.setItem('logged',"loggedIn");

then redirect user to ' / ' the main page.
Everything is good until now.
However, if I changed the URL manually it would access to main page directly!
so how to check if user logged in before loading main page?
I realised I should use sessionStorage.getItem and check it but where I should use it in js?

Upvotes: 1

Views: 1211

Answers (1)

Aaryaman Maheshwari
Aaryaman Maheshwari

Reputation: 111

Could you elaborate? Is this a REST API you are making requests to?

If so then in your front end on the index.html page, include a script tag where you run a conditional regarding the loggedIn session you are storing.

If the session does not exist then redirect them to the login page (i.e window.location.href = "/login").

For such use cases, React is a great option to consider, just putting it out there.

I'd like to mention however, this is a very Insecure way of verifying the users authorization. Use a JWT.

Upvotes: 1

Related Questions