summaryrefslogtreecommitdiffstats
path: root/src/services/AuthService.js
blob: 758157ceffdaa26a2fd27cf2f5157b522d4229e3 (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
57
58
59
import axios from "axios";
import Vue from 'vue'

const url = 'http://localhost:8000/api/auth/'

const error_notify = (error) => {
    for (const [key, values] of Object.entries(error.response.data)) {
        console.log(values)
        values.forEach(value => {
            Vue.notify({
                type: 'error',
                title: value,
                text: (key !== 'non_field_errors') ? key : ''
            })
        })
    }
    return {
        data: error.response.data,
        status: error.response.status
    }
}

export default {
    async login(data) {
        return await axios
        .post(url+'login/', data)
        .then(res => {
            return {
                data: res.data,
                status: res.status
            }
        })
        .catch(error => error_notify(error))
    },

    async register(data) { 
        return await axios
        .post(url+'register/', data)
        .then(res => {
            return {
                data: res.data,
                status: res.status
            }
        })
        .catch(error => error_notify(error))

    },

    async logout() {
        return await axios
        .post(url+'logout/')
        .then(res => {
            return {
                status: res.status
            }
        })
        .catch(error => error_notify(error))
    }
}