aboutsummaryrefslogtreecommitdiffstats
path: root/src/browser/src/views/Login.vue
blob: ec488786d8be02e3eda57d80239bf63c9479b926 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<template>
    <div class="container max-width-xs padding-y-lg">
        <div v-if="isError">
            <h1 v-bind="error.title"></h1>
            <p v-bind="error.message"></p>
        </div>
        <div v-if="isLoading" id="loading-message">
            <p>Logger inn...</p>
        </div>
        <form class="login-form" onsubmit="return false;" @submit="submitForm()">
            <div class="text-component text-center margin-bottom-sm">
                <h1>money-manager</h1>
            </div>
            <div class="margin-bottom-sm">
                <label class="form-label margin-bottom-xxxs" for="username">Email</label>
                <input
                    class="form-control width-100%"
                    type="email"
                    name="username"
                    placeholder="Email"
                    v-model.lazy="this.input.username"
                    id="username"
                />
            </div>

            <div class="margin-bottom-sm">
                <div class="flex justify-between margin-bottom-xxxs">
                    <label class="form-label" for="password">Password</label>
                    <span class="text-sm is-hidden"><a href="#0">Forgot?</a></span>
                </div>

                <input
                    type="password"
                    class="form-control width-100%"
                    name="password"
                    id="password"
                    v-model.lazy="this.input.password"
                    placeholder="Password"
                />
            </div>

            <div class="margin-bottom-sm">
                <button class="btn btn--primary btn--md width-100%">Login</button>
            </div>

            <div class="text-center is-hidden">
                <p class="text-sm">Don't have an account? <a href="#0">Get started</a></p>
            </div>
        </form>
    </div>
</template>

<script>
import constants from "../constants";
import { reactive, toRefs } from "vue";

export default {
    setup() {
        const model = reactive({
            isError: false,
            isLoading: false,
            error: {
                title: "",
                message: "",
            },
            input: {
                username: "",
                password: "",
            },
        });

        function sumitForm(e) {
            console.log("SWOOOSH");
            document.getElementById("password");
            e.preventDefault();
            if (model.input.username || model.input.password) {
                loginAsync();
            }
        }

        function displayError(title = "En feil oppstod", message = "Prøv igjen senere") {
            model.isLoading = false;
            model.error.title = title;
            model.error.message = message;
            model.isError = true;
        }

        async function loginAsync() {
            try {
                model.isLoading = true;

                let response = await fetch(constants.API_ADDRESS + "/account/login", {
                    method: "POST",
                    body: JSON.stringify(model.input),
                });

                let data = await response.json();

                console.log(response);
                console.log(data);

                model.isLoading = false;

                this.$store.commit("setLoggedInState", true);
                this.$router.replace("/");
            } catch (err) {
                displayError(err.title, err.message);
                console.error(err);
            } finally {
                model.isLoading = false;
            }
        }

        return { ...toRefs(model), submitForm };
    },
};
</script>