After the user is logged in i dont want the user to access the log in page. I have tried this one down below but it's seem like it's not working. What should I do to make it works?
[Updated]
When I console.log(store.getters.loggedIn
inside else if
statement it returns false so I think the problem maybe is with store.getters
. Since I'm a beginner in Vuejs I'm really not sure how to fix it. I will add my vuex code down below. Hope I can get some helps.
Thanks before hand!
// vuex
export default new Vuex.Store({
state: {
accessToken: null,
refreshToken: null,
APIData: "",
},
mutations: {
updateStorage(state, { access, refresh }) {
state.accessToken = access;
state.refreshToken = refresh;
},
destroyToken(state) {
state.accessToken = null;
state.refreshToken = null;
},
},
getters: {
loggedIn(state) {
return state.accessToken != null;
},
},
actions: {
userLogout(context) {
console.log("hello");
if (context.getters.loggedIn) {
context.commit("destroyToken");
}
},
userLogin(context, usercredentials) {
return new Promise((resolve, reject) => {
getAPI
.post("/rest-api-token/", {
email: usercredentials.email,
password: usercredentials.password,
})
.then((response) => {
context.commit("updateStorage", {
access: response.data.access,
refresh: response.data.refresh,
});
resolve();
console.log(this.getters.loggedIn); <-- return true
})
.catch((err) => {
reject(err);
});
});
},
},
modules: {},
});
// main.js
router.beforeEach((to, from, next) => {
if (to.matched.some((record) => record.meta.requiresLogin)) {
if (!store.getters.loggedIn) {
next({ name: "login" });
}
else {
next();
}
}
// if user is logged in and go to log in page redirect them to home page
else if (to.matched.some(record => record.meta.requiresVisitor)) {
if (store.getters.loggedIn) {
next({ name: 'home'});
}
else {
next();
}
}
//
else {
next();
}
});
//index.js (router)
const routes = [
{
path: "/",
name: "login",
component: Login,
meta: {
requiresVisitor: true,
},
},
{
path: "/home",
name: "home",
component: Home,
meta: {
requiresLogin: true,
},
},
{
path: "/logout",
name: "logout",
component: Logout,
},
];
i would add a state in my store called:
state: {
accessToken: null,
refreshToken: null,
APIData: "",
loggedIn: false //<<<<<<<<<<<<<<<<<<<<< HERE
},
then i would add a mutation for the Login Process, if the User Loggedin successfully
mutations: {
updateStorage(state, { access, refresh }) {
state.accessToken = access;
state.refreshToken = refresh;
},
destroyToken(state) {
state.accessToken = null;
state.refreshToken = null;
},
loginAttempt(state, payload) { //<<<<<<<<<<<<<<HERE
state.loggedIn = payload
}
},
and toggle the login state to true if success or false if failed.
then i add a v-if="userIsLoggedIn" in your login component and be toggle the Login if user is not logged in and show the Login Component or hide it
of course the userIsLoggedIn
should be a computed:
computed: {
userIsLoggedIn: function() {
return this.$store.state.loggedIn
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.