Warm tip: This article is reproduced from stackoverflow.com, please click
javascript local-storage redirect vue.js window.location

window.location removed the token from localStorage

发布于 2020-04-10 10:14:23
login(data){
    let appUrl = process.env.MIX_APP_PRO + process.env.MIX_APP_URL;
    axios.post('/api/auth/login', data)
    .then(res => {
        this.responseAfterLogin(res)
    })
    .then(dat => {
       let appUrl = '/hub';
       window.location = appUrl;
    })
    .catch(function(error){
        if(error.response){
            Exception.handle(error.response.data)
        }
    })
}
responseAfterLogin(res){
    const access_token = res.data.access_token;
        const username = res.data.user;
        if(Token.isValid(access_token)){
            AppStorage.store(username, access_token);
        }
    }
}

Above are the functions that I am using in order to login a user in to my system and storing the access token to localStorage, The issue however is when user is redirected after successfull login the token some how get deleted from local storage.

Please Help.

import Token from "./Token";

class AppStorage{
    storeToken(token){
        window.localStorage.setItem('token', token);
    }

    storeUser(user){
        window.localStorage.setItem('user', user);
    }

    store(user, token){
        this.storeToken(token);
        this.storeUser(user);
    }

    clear(){
        window.localStorage.removeItem('user');
        window.localStorage.removeItem('token');
    }

    getToken(){
        return window.localStorage.getItem('token');
    }

    getUser(){
        return window.localStorage.getItem('user');
    }

}

export default AppStorage = new AppStorage()

This is App storage class I am using to store and retrieve values in localStorage.

Questioner
Imran Ahmed
Viewed
64
Imran Ahmed 2020-02-17 18:21

Guys this problem was solved, it was my mistake.

When I uploaded the site on the server I need to generate key for JWT key on time of installation of JWT Modules, which I didn't.

So the user was not logging in properly thats why token was being removed form localStorage as result of un-successful authentication.

Thanks for all the help guys.