Skip to content

Commit 8a35cad

Browse files
committed
User Login lesson code
1 parent 4160398 commit 8a35cad

File tree

8 files changed

+100
-3
lines changed

8 files changed

+100
-3
lines changed

src/components/AppNav.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66
<router-link to="/dashboard">
77
Dashboard
88
</router-link>
9+
<router-link v-if="!loggedIn" to="/login" class="button">
10+
Login
11+
</router-link>
912
</div>
1013
</template>
1114

1215
<script>
13-
export default {}
16+
import { authComputed } from '../vuex/helpers.js'
17+
export default {
18+
computed: {
19+
...authComputed
20+
}
21+
}
1422
</script>
1523

1624
<style lang="scss" scoped>

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from 'vue'
22
import App from './App.vue'
33
import router from './router'
4-
import store from './store'
4+
import store from './vuex/store'
55

66
Vue.config.productionTip = false
77

src/router.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Router from 'vue-router'
33
import Home from './views/Home.vue'
44
import Dashboard from './views/Dashboard.vue'
55
import RegisterUser from './views/RegisterUser.vue'
6+
import LoginUser from './views/LoginUser.vue'
67

78
Vue.use(Router)
89

@@ -24,6 +25,11 @@ const router = new Router({
2425
path: '/register',
2526
name: 'register',
2627
component: RegisterUser
28+
},
29+
{
30+
path: '/login',
31+
name: 'login',
32+
component: LoginUser
2733
}
2834
]
2935
})

src/views/Home.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
<template>
22
<div class="home">
33
<h1>Welcome to the App!</h1>
4+
<div v-if="!loggedIn">
5+
To use this app you'll need to
6+
<router-link to="/login">
7+
Login
8+
</router-link>
9+
or
10+
<router-link to="/register">
11+
Register
12+
</router-link>
13+
</div>
414
</div>
515
</template>
616

717
<script>
8-
export default {}
18+
import { authComputed } from '../vuex/helpers.js'
19+
export default {
20+
computed: {
21+
...authComputed
22+
}
23+
}
924
</script>

src/views/LoginUser.vue

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<template>
2+
<div>
3+
<form @submit.prevent="login">
4+
<label for="email">
5+
Email:
6+
</label>
7+
<input v-model="email" type="email" name="email" value>
8+
9+
<label for="password">
10+
Password:
11+
</label>
12+
<input v-model="password" type="password" name="password" value>
13+
14+
<button type="submit" name="button">
15+
Login
16+
</button>
17+
<router-link to="/register">
18+
Don't have an account? Register.
19+
</router-link>
20+
</form>
21+
</div>
22+
</template>
23+
24+
<script>
25+
export default {
26+
data () {
27+
return {
28+
email: '',
29+
password: ''
30+
}
31+
},
32+
methods: {
33+
login () {
34+
this.$store
35+
.dispatch('login', {
36+
email: this.email,
37+
password: this.password
38+
})
39+
.then(() => {
40+
this.$router.push({ name: 'dashboard' })
41+
})
42+
}
43+
}
44+
}
45+
</script>
46+
47+
<style scoped>
48+
</style>

src/views/RegisterUser.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<button type="submit" name="button">
2020
Register
2121
</button>
22+
<router-link to="/login">
23+
Already have an account? Login.
24+
</router-link>
2225
</form>
2326
</div>
2427
</template>

src/vuex/helpers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { mapGetters } from 'vuex'
2+
3+
export const authComputed = {
4+
...mapGetters(['loggedIn'])
5+
}

src/store.js renamed to src/vuex/store.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ export default new Vuex.Store({
2424
.then(({ data }) => {
2525
commit('SET_USER_DATA', data)
2626
})
27+
},
28+
login ({ commit }, credentials) {
29+
return axios
30+
.post('//localhost:3000/login', credentials)
31+
.then(({ data }) => {
32+
commit('SET_USER_DATA', data)
33+
})
34+
}
35+
},
36+
getters: {
37+
loggedIn (state) {
38+
return !!state.user
2739
}
2840
}
2941
})

0 commit comments

Comments
 (0)