0

I am using Google Sign-In and have the following problem:

sessionStorage.getItem('userEntity') returns null when I close the browser and open it again. It does not return null if I just reload the page.

The session will be set in this function:

// Signing in
function onSignIn(googleUser) {
    var profile = googleUser.getBasicProfile();

    // Store the entity object in sessionStorage where it will be accessible from all pages
    let userEntity = {};
    userEntity.id = profile.getId();
    userEntity.name = profile.getName();
    userEntity.img = profile.getImageUrl();
    userEntity.email = profile.getImageUrl();
    sessionStorage.setItem('userEntity',JSON.stringify(userEntity));

    userIsLoggedOut.style.display = 'none';
    userIsLoggedIn.style.display = 'flex';
    document.querySelector('.user-img').setAttribute('src', userEntity.img);
}

I do not run this function in JavaScript. The following HTML will make it run:

<div class="g-signin2" data-onsuccess="onSignIn"></div>

Even if I put console.log(sessionStorage.getItem('userEntity')) after the function or on the bottom of my JavaScript, it still returns null.

I guess a solution would be using Promise but how can I use it if I don't run the function onSignIn() in JavaScript?

4
  • 2
    I think you should be using local storage if you want data to persist between browser restarts. This is explained in the documentation: developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage Commented Jul 1, 2019 at 23:00
  • 1
    Firstly, onSignIn is javascript, therefore it iis "run in javascript". But you're absolutely incorrect in thinking that Promises would help make "sessionStorage" persist over browser restarts, because, well, nothing will Commented Jul 1, 2019 at 23:04
  • As the comment from Jake said, you might want to use Local Storage instead of Session Storage. If you actually need Session Storage, are you sure the setItem method is being called? Have you debugged your onSignIn method? An easy way to debug it is just put debugger; in your javascript, and that should cause a breakpoint to be hit Commented Jul 1, 2019 at 23:05
  • @JakeHolzinger thank you! localStorage was definitely the solution! Commented Jul 1, 2019 at 23:08

2 Answers 2

1

The data stored in SessionStorage are deleted when you close the tab or the browser, more details below: https://www.w3schools.com/jsref/prop_win_sessionstorage.asp

You can use localStorage instead to keep the data in the browser even if you close the tab or the browser, so change the following line:

sessionStorage.setItem('userEntity',JSON.stringify(userEntity));

by

localStorage.setItem('userEntity',JSON.stringify(userEntity));

But you have to take care about the security of the informations stored in the localStorage, you can deal with secure cookies if you need to store secure data client side.

Sign up to request clarification or add additional context in comments.

Comments

1

You can copy item from sessionStorage to localStorage:

localStorage.setItem('userEntity', sessionStorage.getItem('userEntity'));

It will be persistent, so you can use it anytime:

localStorage.getItem('userEntity');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.