Browse Source

💚 Check if user is already logged in

main
Haris Razis 4 years ago
parent
commit
d38fa37713
  1. 3
      web/src/main.ts
  2. 10
      web/src/store/index.ts
  3. 11
      web/src/views/Home.vue
  4. 3
      web/src/views/Login.vue

3
web/src/main.ts

@ -8,5 +8,6 @@ import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import '@fortawesome/fontawesome-free/js/all.js';
axios.defaults.withCredentials = true;
axios.defaults.baseURL = 'http://localhost:8000';
createApp(App).use(store).use(router).mount('#app')

10
web/src/store/index.ts

@ -26,7 +26,7 @@ export default createStore({
return new Promise((resolve, reject) => {
axios({
method: 'post',
url: 'http://localhost:8000/auth/login',
url: '/auth/login',
data: qs.stringify({...user})
})
.then((resp: AxiosResponse) => {
@ -43,7 +43,7 @@ export default createStore({
return new Promise((resolve, reject) => {
axios({
method: 'post',
url: 'http://localhost:8000/auth/logout'
url: '/auth/logout'
})
.then((resp: AxiosResponse) => {
commit("logout");
@ -56,19 +56,17 @@ export default createStore({
})
})
},
getUser({commit}) {
getCurrentUser({commit}) {
return new Promise((resolve, reject) => {
axios({
method: 'get',
url: 'http://localhost:8000/auth/current_user'
url: '/auth/current_user'
})
.then((resp: AxiosResponse) => {
console.log(resp)
commit("auth_success", resp.data);
resolve(resp)
})
.catch((err: Error) => {
console.log(err)
commit('auth_error')
reject(err)
})

11
web/src/views/Home.vue

@ -48,5 +48,16 @@ import ShapeDividerTop from '@/components/ShapeDividerTop.vue';
})
export default class Home extends Vue {
mounted() {
this.getCurrentUser();
}
private getCurrentUser() {
this.$store.dispatch('getCurrentUser')
.then(() => this.$router.push('/dashboard'))
.catch((err: any) => {
this.$router.push('/')
})
}
}
</script>

3
web/src/views/Login.vue

@ -61,7 +61,8 @@ export default class Login extends Vue {
this.$store.dispatch('login', user)
.then(() => this.$router.push('/dashboard'))
.catch((err: any) => {
this.msg = err.response.data.errors.message || err.message
console.log(err)
this.msg = err.response.data.errors.message || err.message || 'Something went wrong!'
this.$router.push('/login')
})
}

Loading…
Cancel
Save