Próbując zrobić logowanie po stronie Vue.js i przeglądając różne rozwiązania wymyśliłem coś takiego. Teraz moje pytanie....na ile to jest poprawne?
Login.vue
<script>
import {mapGetters} from 'vuex'
export default {
data() {
return {
title: 'Zaloguj się',
credential: {
email: '',
password: ''
}
}
},
computed: {
...mapGetters({
logged: 'logged'
})
},
methods: {
login(){
this.$store.dispatch('login', this.credential);
}
}
}
</script>
login.js
import router from '../../router'
const types = {
LOGIN: 'LOGIN',
LOGOUT: 'LOGOUT'
}
const state = {
logged: localStorage.getItem('token')
}
const getters = {
isLogged: state => state.logged
}
const actions = {
login({commit}, credential) {
axios.post('login', credential)
.then((result) => {
localStorage.setItem('token', result.data.access_token);
commit(types.LOGIN);
router.push({path: '/'});
});
},
logout({commit}) {
axios('logout')
.then(() => {
localStorage.removeItem('token');
commit(types.LOGOUT);
router.push({path: '/login'});
});
}
}
const mutations = {
[types.LOGIN](state) {
state.logged = 1;
},
[types.LOGOUT](state) {
state.logged = 0;
}
}
export default {
state,
getters,
actions,
mutations
}
w sumie działa ok, token jest zapisywany do Local Storage, tylko nwm jak z poprawnością :/