Nuxt/examples/auth-routes/pages/index.vue

60 lines
1.5 KiB
Vue
Raw Normal View History

2016-12-04 18:16:10 +00:00
<template>
<div class="container">
<h1>Please login to see the secret content</h1>
2016-12-07 16:03:52 +00:00
<form v-if="!$store.state.authUser" @submit.prevent="login">
2016-12-04 18:16:10 +00:00
<p class="error" v-if="formError">{{ formError }}</p>
<p><i>To login, use <b>demo</b> as username and <b>demo</b> as password.</i></p>
<p>Username: <input type="text" v-model="formUsername" name="username" /></p>
<p>Password: <input type="password" v-model="formPassword" name="password" /></p>
<button type="submit">Login</button>
</form>
<div v-else>
2016-12-07 16:03:52 +00:00
Hello {{ $store.state.authUser.username }}!
2016-12-04 18:16:10 +00:00
<pre>I am the secret content, I am shown only when the use is connected.</pre>
2016-12-07 16:03:52 +00:00
<p><i>You can also refresh this page, you'll still be connected!</i></p>
<button @click="logout">Logout</button>
2016-12-04 18:16:10 +00:00
</div>
2016-12-16 17:12:38 +00:00
<p><nuxt-link to="/secret">Super secret page</nuxt-link></p>
2016-12-04 18:16:10 +00:00
</div>
</template>
<script>
export default {
2016-12-07 16:03:52 +00:00
data () {
2016-12-04 18:16:10 +00:00
return {
formError: null,
formUsername: '',
formPassword: ''
}
},
methods: {
login () {
2016-12-07 16:03:52 +00:00
this.$store.dispatch('login', {
username: this.formUsername,
password: this.formPassword
2016-12-04 18:16:10 +00:00
})
2016-12-07 16:03:52 +00:00
.then(() => {
this.formUsername = ''
this.formPassword = ''
this.formError = null
2016-12-04 18:16:10 +00:00
})
2016-12-07 16:03:52 +00:00
.catch((e) => {
this.formError = e.message
2016-12-04 18:16:10 +00:00
})
2016-12-07 16:03:52 +00:00
},
logout () {
this.$store.dispatch('logout')
2016-12-04 18:16:10 +00:00
}
}
}
</script>
<style>
.container {
padding: 100px;
}
.error {
color: red;
}
</style>