Skip to content
This repository was archived by the owner on Sep 24, 2024. It is now read-only.

Commit eb9ba37

Browse files
committed
Refactor the login page to use composition api
1 parent f95dbef commit eb9ba37

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

src/views/Login.vue

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,40 +58,44 @@
5858
</template>
5959

6060
<script>
61-
import { mapMutations, mapActions } from 'vuex'
61+
import { ref } from '@vue/composition-api'
6262
6363
export default {
64-
data() {
65-
return {
66-
email: undefined,
67-
password: undefined,
68-
error: undefined
64+
name: 'Login',
65+
setup(props, context) {
66+
const { $store } = context.root
67+
68+
const email = ref('')
69+
const password = ref('')
70+
71+
const error = ref(null)
72+
function dismissError() {
73+
error.value = null
6974
}
70-
},
71-
methods: {
72-
dismissError() {
73-
this.error = undefined
74-
this.clearAuthenticateError()
75-
},
7675
77-
onSubmit(email, password) {
78-
this.authenticate({ strategy: 'local', email, password })
76+
function onSubmit(email, password) {
77+
$store
78+
.dispatch('auth/authenticate', { strategy: 'local', email, password })
7979
// Just use the returned error instead of mapping it from the store.
80-
.catch(error => {
80+
.catch(err => {
8181
// Convert the error to a plain object and add a message.
82-
let type = error.className
83-
error = Object.assign({}, error)
84-
error.message =
82+
let type = err.className
83+
err = Object.assign({}, err)
84+
err.message =
8585
type === 'not-authenticated'
8686
? 'Incorrect email or password.'
8787
: 'An error prevented login.'
88-
this.error = error
88+
this.error = err
8989
})
90-
},
91-
...mapMutations('auth', {
92-
clearAuthenticateError: 'clearAuthenticateError'
93-
}),
94-
...mapActions('auth', ['authenticate'])
90+
}
91+
92+
return {
93+
email,
94+
password,
95+
error,
96+
dismissError,
97+
onSubmit
98+
}
9599
}
96100
}
97101
</script>

0 commit comments

Comments
 (0)