'Nuxtjs. loggedIn is still false after setUser
I have two websites. On the first the user logs in (creating a local storage value), on the second (the one I am trying to program) the storage value is rechecked for login but is returning false. What do I need to modify?
NB: I know this is a strange situation, but there is a reason for it so it cannot be changed.
In strategies I have...
local: {
token: { required: false, maxAge: 90000 },
user: { autoFetch: false, property: false },
endpoints: {
user: false,
login: false,
logout: false,
},
},
... property being set to false as the storage value is a more complex version of {"firstName":"abc","lastName":"def"}, i.e. not in a user field.
In the main vue file I have...
created() {
var savedData = localStorage.getItem("ngStorage-user");
if (savedData !== null) {
alert(savedData); // Displays the string that I expect.
this.$auth.setUser(JSON.parse(savedData)); // The setUser call.
alert(this.$auth.user.firstName); // Displays 'abc'.
alert(this.$auth.loggedIn); // Displays false
}
}
PS: I have reviewed the similar queries, but none seems to match this.
====
Addendum: I have possibly solved this, and improved it at the same time by checking that the token is valid.
Added a call to tokencheck in the API which checks the token and returns minimal user details.
local: {
token: { required: false, maxAge: 90000, property: "token" },
user: { autoFetch: false, property: false },
endpoints: {
user: { url: `${process.env.API_URL}tokencheck` },
login: false,
logout: false,
},
},
And then in Vue
created() {
var savedData = localStorage.getItem("ngStorage-user");
if (savedData !== null) {
var parsedSaveData = JSON.parse(savedData);
this.$auth.setUserToken(parsedSaveData.token) // Which calls the 'user' endpoint
.then(res => { this.$auth.setUser(res.data); })
.catch(() => { this.error = true; });
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
