summaryrefslogtreecommitdiffstats
path: root/src/views/Login.vue
blob: ae43ecd69859715f959961d1accc7663907cc839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<template>
  <div id="login">
      <form @submit="log_in">
        <input type="text" placeholder="Login" v-model="login">
        <input type="password" placeholder="Password" v-model="password">

        <input type="submit" value="Login!">
      </form>
      <p v-for="(mess, key) in messages" :key="key">{{mess}}</p>

      <router-link to="/register"> Still don't have account? </router-link>
      
    </div>
</template>

<script>
import AuthService from '@/services/AuthService'

export default {
    data: () => {
        return {
            login: "",
            password: "",
            messages: []
        }
    },
    methods: {
        async log_in(e) {
            e.preventDefault()

            const data = {
                "username": this.login,
                "password": this.password
            }
            try{
                const res = await AuthService.login(data)
                this.$store.dispatch('login', {token: res.token, user: res.user})
                this.$router.push('/') 
            } catch (error) {
                console.log(error.response.data)
                this.messages.push('Wrong username or password!')
            }
        }
    },
    created() {
        if(this.$store.getters['is_logged_in']) {
            this.$router.push('/')
        }
    }

}
</script>

<style>

</style>