-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLoginForm.vue
More file actions
70 lines (67 loc) · 1.83 KB
/
LoginForm.vue
File metadata and controls
70 lines (67 loc) · 1.83 KB
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
<script setup>
import axios from 'axios';
</script>
<template>
<div class="loginForm">
<template v-if="!authenticated">
<label>Username: <input type="text" v-model="model.username" /></label>
<label>Password: <input type="password" v-model="model.password" /></label>
<button type="button" v-on:click="sendLoginData">Submit</button>
<small>(Hint: Use admin, password 1111 or user, password 1111)</small>
</template>
<template v-else>
Angemeldet als {{ username }}
<a href="javascript:void(0)" v-on:click="deleteToken">Abmelden</a>
</template>
</div>
</template>
<style scoped>
.loginForm {
display: flex;
gap:1rem;
}
a {
text-decoration: none;
}
a:hover {
color: lightgray;
text-decoration: underline;
}
</style>
<script>
export default {
data() {
return {
model: {
username: '',
password: '',
},
};
},
methods: {
deleteToken() {
delete axios.defaults.headers.common['Authorization'];
this.$store.commit('authenticate', null);
},
async sendLoginData() {
try {
const userdata = (await axios.post('user/login', this.model)).data;
axios.defaults.headers.common['Authorization'] = `Bearer ${userdata.token}`;
this.$store.commit('authenticate', userdata);
} catch (e) {
if (e.response.status == 401) {
alert('Login failed. Invalid credentials.');
}
}
},
},
computed: {
authenticated() {
return this.$store.state.user.isLoggedIn;
},
username() {
return this.$store.state.user.name;
}
},
};
</script>