Issues accessing localStorage with JavaScript - javascript

I'm trying to run a function when body loads, that will check if the user is logged in, and if not redirect them to the login page. Here is the login function :
function login() {
var mail = document.getElementById('mail').value; //get values
var psw = document.getElementById('psw').value; //get values
localStorage.setItem('logged_in', true); //specify that user is logged in
localStorage.setItem('mail', mail); //store mail
window.location.replace('pages/home.html'); //redirect to home page
}
And the function to check if the user is logged in :
function check_logged_in() {
const logged_in = localStorage.getItem('logged_in');
if (logged_in == null) { //check if user is logged in
alert('You are not logged in, you are about to be redirected. '); //alert user
window.location.replace("../index.html"); //redirect to login page
}
}
The problem is that even if the login function run before, I am redirected. I think that the localStorage resets on each redirection. If that is the problem, do you know the way to prevent this, or if it isn't the problem, do you know what it might be?

Based on the documentation a localStorage item can either be null (when it's empty) or a string. It does not store any other data types.
The expression
localStorage.setItem("logged_in", true);
Does not save a boolean value to the localStorage item. instead, it saves the string value "true"

Related

How to make a onclick redirect that only redirects if you're signed in?

I've made a sign up sign in page using firebase but I have a button to a second page that I only want people who are signed in to access. I want it so if you aren't signed in it sends an alert but if you are it just redirects you. At the moment the alert comes up whether you're signed in or not so it's not recognising the user. Any help would be greatly appreciated, this is my first time using firebase.
I have a button in my html with an onclick function and this is the function it is calling.
var user = firebase.auth().currentUser;
function homepage() {
if(user) {
window.location = '/homepage.html';
} else {
alert("Please Sign In");
}
}
Have you tried putting the var user = ... inside the homepage() function? I suspect the variable gets assigned when this script is initially loaded, but when the function is called (at a time when the user might have subsequently authenticated), the user variable is not assigned the updated value.

ExtJS 5.1 Remember Me on local store

I'm trying to do Remember Me with an interesting algorithm. I was thinking, when i click the Remember Me and Login, the value of the text fields (username&password) will be saved as a default value. My login button click event here:
var username = Ext.getCmp('setNo').getValue();
var password= Ext.getCmp('setPass').getValue();
if(refs.checkStudent.value === true){
Ext.getCmp('setNo').setValue(username);
Ext.getCmp('setPass').setValue(password);
}
else{
Ext.getCmp('setNo').setValue("");
Ext.getCmp('setParola').setValue("");
}
On console, it is working. But I'm working with a local store, no server. So when i refresh the page, it's gone. Is there a way to not lose them?
on your view onAfterRender event:
Ext.getCmp('setNo').setValue(localStorage.username);
Ext.getCmp('setPass').setValue(localStorage.password);
on your button click event:
if (refs.checkStudent.value === true) {
localStorage.username = Ext.getCmp('setNo').getValue();
localStorage.password = Ext.getCmp('setPass').getValue();
} else {
localStorage.removeItem('username');
localStorage.removeItem('password');
}
Use ExtJs utility to set and get values in cookies. At time of login set
username and password in cookies and after refresh the page read username
and password value from the cookie.
Ext.util.Cookies.set('username', username); // To set value in cookie.
Ext.util.Cookies.get('username'); // to get value form cookie.

Meteor.user() is null when trying to check user login status

I am trying to show other some user's profile page to non-logged in and logged-in users. The problem is that whenever I check
if(Meteor.user() === me)
Meteor.user() returns null and code crashes since no one is logged in. How can I check whether there are logged in users?
Try this and let me know:
if (Meteor.user()) {
// code for login user
} else {
// code for non-login user
}

localStorage for phonegap will store stuff after exiting application?

How can I use localStorage to store login credentials for my application? I'm thinking it will store it somewhere when I exit the app, and then when I open the application again the fields will be prefilled with the information from localStorage. Here is my code so far.
function onDeviceReady() {
alert("ready");
var email = window.localStorage.getItem("email");
var password = window.localStorage.getItem("password");
document.getElementById("email").value = email;
document.getElementById("password").value = password;
}
//If checkbox gets checked then save credentials, if unchecked then forget
function rememberMe() {
if(document.getElementById('remember_me').checked) {
alert("checked");
window.localStorage.setItem("email", document.getElementById("email").value);
window.localStorage.setItem("password", docuement.getElementById("password").value);
}
else {
alert("unchecked");
window.localstorage.clear();
}
}
Best way to do it is use webSQL or SQLite database. save credentials when anybody tries to login and then when someone opens the app, fill the boxes by reading data from database on pageload or device ready function.
localStorage can hold object´s data until it gets removed from the same, but sessionStorage will clear the data when you close the tab/browser.

Facebook login redirect issue

I have a site with a working Facebook login.
The problem is happens when the user comes to the site for the first time (or after clearing cookies).
The user flow is the following:
User clicks "Sign Up with Facebook"
User is redirected to Facebook authentication
If User accepts, he/she is redirected to the 'Sign Up' page.
The problem is that if the person is doing this for the first time, they are always redirected back to the home page.
There are only 2 possible views that can be given for this url
The Sign Up page, with only some form data sent as parameters
An alert box upon successful DB persistence
Thus, I have ruled out PHP as a possible cause of the redirect.
I have included the JavaScript managing the Facebook interaction.
All other relevant code can be found in the HTML of the page.
The Page can be found at http://trioisrael.com
function fb_login(){
FB.login(function(response) {
if (response.authResponse) {
window.location = "http://trioisrael.com/signup"
//console.log(response); // dump complete info
access_token = response.authResponse.accessToken; //get access token
user_id = response.authResponse.userID; //get FB UID
FB.api('/me', function(response) {
user_email = response.email; //get user email
// you can store this data into your database
});
} else {
//user hit cancel button
alert('We use facebook to make sure that everyone is really who they say they are');
}
}, {
scope: 'user_photos,user_birthday'
});
}
Trying to do additional stuff after assigning a new value to window.location (which should be window.location.href, of course) does not make sense.
If you want to do additional stuff after login, do it before redirecting the user somewhere else.

Categories

Resources