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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
<template>
<div class="container max-width-xs padding-y-lg">
<form class="login-form" @submit.prevent="submitForm()">
<div class="text-component text-center margin-bottom-sm">
<h1>dough</h1>
</div>
<Alert v-if="isError" :title="error.title" :message="error.message" type="error" />
<div class="margin-bottom-sm">
<label class="form-label margin-bottom-xxxs" for="username">Email</label>
<input
class="form-control width-100%"
type="text"
name="username"
id="username"
:disabled="isLoading == true"
v-model.trim="input.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"
:disabled="isLoading == true"
v-model.trim="input.password"
/>
</div>
<div class="margin-bottom-sm">
<button class="btn btn--primary btn--md btn--preserve-width">
<span v-if="isLoading" class="btn__content-b">
<svg class="icon icon--is-spinning" aria-hidden="true" viewBox="0 0 16 16">
<title>Loading</title>
<g stroke-width="1" fill="currentColor" stroke="currentColor">
<path
d="M.5,8a7.5,7.5,0,1,1,1.91,5"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
></path>
</g>
</svg>
</span>
<span v-else class="btn__content-a">
Login
</span>
</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 Alert from "../components/Alert/Alert.vue";
import FillLoader from "../components/FillLoader/FillLoader.vue";
import constants from "../constants";
import { reactive, toRefs } from "vue";
import store from "../store";
import router from "../router";
import account from "../api/account";
export default {
components: {
Alert,
FillLoader,
},
setup() {
const model = reactive({
isError: false,
isLoading: false,
error: {
title: "",
message: "",
},
input: {
username: "",
password: "",
},
});
async function submitForm() {
document.getElementById("username").required = true;
document.getElementById("password").required = true;
if (model.input.username && model.input.password) {
model.isLoading = true;
model.isError = false;
try {
let loginResponse = await account.loginAsync(
model.input.username,
model.input.password
);
if (loginResponse.status === 200) {
let profileResponse = await account.getProfileAsync();
let profileData = await profileResponse.json();
if (profileResponse.status === 200) {
model.isLoading = false;
store.commit("setProfileData", profileData);
router.replace("/");
} else {
displayError(profileData.title, profileData.message);
}
} else {
let errorData = await loginResponse.json();
displayError(errorData.title, errorData.message);
}
} catch (error) {
console.error(error);
displayError();
}
}
}
function displayError(
title = "An unknown error ocurred",
message = "Please try again soon"
) {
model.isLoading = false;
model.error.title = title;
model.error.message = message;
model.isError = true;
}
return { ...toRefs(model), submitForm };
},
};
</script>
<style lang="scss"></style>
|