Skip to content

Commit 09e49c4

Browse files
committed
feature: login actions
1 parent 74fdd5e commit 09e49c4

File tree

4 files changed

+53
-27
lines changed

4 files changed

+53
-27
lines changed

auth/src/App.vue

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,13 @@
1919
Profile
2020
</router-link>
2121
</div>
22-
23-
<div class="navbar-end">
24-
<div class="navbar-item">
25-
<div class="buttons">
26-
<router-link :to="{ name: 'index' }" class="button is-light">
27-
Log in
28-
</router-link>
29-
</div>
30-
</div>
31-
</div>
3222
</div>
3323
</div>
3424
</nav>
35-
<div class="container">
36-
<!-- component matched by the route will render here -->
37-
<router-view></router-view>
38-
</div>
25+
26+
<!-- component matched by the route will render here -->
27+
<router-view></router-view>
28+
3929
<footer class="footer">
4030
<div class="content has-text-centered">
4131
<p>&copy; Copyleft {{ new Date().toLocaleString('it-IT', { year: 'numeric' }) }} Point of Vue</p>
@@ -55,11 +45,6 @@ export default {
5545
5646
html,
5747
body {
58-
width: 100%;
59-
height: 100%;
60-
min-height: 100%;
61-
padding: 0;
62-
margin: 0;
6348
overflow-x: hidden;
6449
}
6550
</style>

auth/src/pages/Login.vue

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
<template>
2-
<div class="container">
3-
4-
</div>
2+
<section class="hero is-medium">
3+
<div class="hero-body">
4+
<div class="container has-text-centered">
5+
<div class="column is-4 is-offset-4">
6+
<p class="subtitle has-text-black">Please login to proceed.</p>
7+
<div class="box">
8+
<form @submit.prevent="loginSubmit">
9+
<div class="field">
10+
<div class="control">
11+
<input v-model="email" class="input" type="text" placeholder="Your Email" autofocus="">
12+
</div>
13+
</div>
14+
<div class="field">
15+
<div class="control">
16+
<input v-model="password" class="input" type="password" placeholder="Your Password">
17+
</div>
18+
</div>
19+
<button class="button is-block is-info is-fullwidth">Login</button>
20+
</form>
21+
</div>
22+
</div>
23+
</div>
24+
</div>
25+
</section>
526
</template>
627

728
<script>
@@ -10,8 +31,18 @@ export default {
1031
1132
data() {
1233
return {
13-
34+
email: null,
35+
password: null,
1436
};
1537
},
38+
39+
methods: {
40+
loginSubmit() {
41+
this.$store.dispatch('doLogin', {
42+
email: this.email,
43+
password: this.password,
44+
});
45+
}
46+
}
1647
}
1748
</script>

auth/src/router.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ const router = new VueRouter({
3333
* Redirect to login if the user is not authenticated
3434
*/
3535
router.beforeEach((to, from, next) => {
36-
if (to.name !== 'login' && !store.state.accessToken) {
37-
next('/login');
36+
if (store.state.accessToken || to.name === 'login' ) {
37+
next();
3838
}
39-
next();
39+
next('/login');
4040
});
4141

4242
Vue.use(VueRouter);

auth/src/store/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Vue from 'vue';
22
import Vuex from 'vuex';
33

4+
import router from '../router';
5+
46
Vue.use(Vuex);
57

68
const user = {
@@ -14,7 +16,12 @@ const user = {
1416
},
1517
mutations: {
1618
updateUser: (state, data) => {
17-
state = data;
19+
state.id = data.id;
20+
state.registeredAt = data.registeredAt;
21+
state.avatar = data.avatar;
22+
state.name = data.name;
23+
state.email = data.email;
24+
state.accessToken = data.accessToken;
1825
},
1926
logout: (state) => {
2027
state.accessToken = null;
@@ -32,10 +39,13 @@ const user = {
3239
});
3340
const response = await rawResponse.json();
3441
commit('updateUser', response);
42+
console.log(response);
43+
router.push('/');
3544
},
3645

3746
doLogout({ commit }) {
3847
commit('logout');
48+
router.push('/');
3949
}
4050
},
4151
getters: {},

0 commit comments

Comments
 (0)