0

I have two javascript files to handle my webpage login function. First js file authenticates the user and returns a session token which should be passed to second js file for session validation.

Here is the snippet of first js file.

var Login = window.Login || {};

(function scopeWrapper($) {
    var apiEndPoint = 'https://4pmsdffwl2.execute-api.ap-southeast-2.amazonaws.com/prod';
    Login.sessionId = 'null'
    function signin(username, password, onSuccess, onFailure) {
        let endpoint = apiEndPoint + '/signin'
        let config = {
                  "username": username,
                  "password": password
          }

        return axios.post(endpoint, config)
          .then((res) => {
            Login.sessionId = res.session_id
            console.log(Login.sessionId) \\this prints the session id correctly
            onSuccess(res);
            return Promise.resolve('/')
          })
          .catch((err) => {
            console.error(err)
            onFailure(err)
            return Promise.reject('/')
          })
    }

    $(function onDocReady() {
        $('#signinForm').submit(handleSignin);
    });

    function handleSignin(event) {
        var email = $('#emailInputSignin').val().toLowerCase();
        var username = email;
        var password = $('#passwordInputSignin').val();
        event.preventDefault();
        signin(username, password, email,
            function signinSuccess() {
                console.log('Successfully Logged In');
                window.location.href = 'second.html';
            },
            function signinError(err) {
                alert(err);
            }
        );
    }

}(jQuery));

The idea is to initiate a global variable sessionId which is updated with the session id by login function.

Here is the snippet of second js file where I'm trying to use the sessionId passed in from the first js file.

var Login = window.Login || {};
Login.map = Login.map || {};

(function uploadScopeWrapper($) {
    console.log(Login.sessionId) \\This prints null instead of sessionId
        if (Login.sessionId) {
            window.location.href = 'welcome.html';
        } else {
            window.location.href = 'login.html';
        }
}(jQuery));

I'm not quite sure why the window object is not getting updated with the sessionId that is being generated in signin function.

Sample code in plnkr here: https://plnkr.co/bYECNY

enter image description here

Any help is much appreciated.

Thanks

autobot
  • 83
  • 1
  • 11
  • Please have a look on this link: https://stackoverflow.com/questions/3244361/can-i-access-variables-from-another-file – Sunil Kumar Sep 30 '19 at 08:26
  • How do you include the files? If they are included using some mechanism that does not directly execute the files but embeds the code into functions, the variable `Login` would not be global. – NineBerry Sep 30 '19 at 08:27
  • 2
    It's really not clear to me, from the question, what exactly isn't working here. Please try and be more clear and include a [mcve] – Liam Sep 30 '19 at 08:27
  • 1
    `console.log(IntAcct.sessionId) \\This prints null instead of sessionId` well what is `IntAct`? You never declare this variable in any of your code? – Liam Sep 30 '19 at 08:29
  • @Liam - Sorry, I have corrected the code. I'm expecting the global variable to be updated with the value from signin function. – autobot Sep 30 '19 at 08:46
  • @NineBerry - I'm including the js files in html code as ``` ``` I guess that execute the files in that order? – autobot Sep 30 '19 at 08:49
  • 2
    The `signin` function isn't called anyhwere – NineBerry Sep 30 '19 at 08:56
  • @NineBerry - I have updated the question. I will add a link to a fiddle which would make my question more clear! Thanks. – autobot Sep 30 '19 at 09:32
  • 1
    Can you please create and **test** a proper [mcve] please. This drip drip of "updated the question" is very annoying – Liam Sep 30 '19 at 09:58
  • I've been able to recreate my question in plunker here https://plnkr.co/bYECNY If the code is downloaded to pc and index.html is executed, the console outputs null for Login.session variable in second.js file upon successful login. I hope that clarifies my question. – autobot Sep 30 '19 at 12:02

1 Answers1

1

You are navigating to a different page and then load the second script file from that different page.

All global variables are lost when you navigate to a different page, the same page or just refresh the current page.

For ways to store information longer than the current document is loaded into the browser, see Persist variables between page loads

NineBerry
  • 20,173
  • 3
  • 50
  • 78
  • Thanks @NineBerry ! I've managed to store the security token using sesionStorage. I need to research on how to make the security token stored in session storage secure (and if it is recommended to store sensitive information in sessionStorage at all). – autobot Sep 30 '19 at 23:41