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

11
web/src/views/Home.vue

@ -48,5 +48,16 @@ import ShapeDividerTop from '@/components/ShapeDividerTop.vue';
}) })
export default class Home extends 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> </script>

3
web/src/views/Login.vue

@ -61,7 +61,8 @@ export default class Login extends Vue {
this.$store.dispatch('login', user) this.$store.dispatch('login', user)
.then(() => this.$router.push('/dashboard')) .then(() => this.$router.push('/dashboard'))
.catch((err: any) => { .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') this.$router.push('/login')
}) })
} }

Loading…
Cancel
Save