|
58 | 58 | </template> |
59 | 59 |
|
60 | 60 | <script> |
61 | | -import { mapMutations, mapActions } from 'vuex' |
| 61 | +import { ref } from '@vue/composition-api' |
62 | 62 |
|
63 | 63 | 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 |
69 | 74 | } |
70 | | - }, |
71 | | - methods: { |
72 | | - dismissError() { |
73 | | - this.error = undefined |
74 | | - this.clearAuthenticateError() |
75 | | - }, |
76 | 75 |
|
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 }) |
79 | 79 | // Just use the returned error instead of mapping it from the store. |
80 | | - .catch(error => { |
| 80 | + .catch(err => { |
81 | 81 | // 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 = |
85 | 85 | type === 'not-authenticated' |
86 | 86 | ? 'Incorrect email or password.' |
87 | 87 | : 'An error prevented login.' |
88 | | - this.error = error |
| 88 | + this.error = err |
89 | 89 | }) |
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 | + } |
95 | 99 | } |
96 | 100 | } |
97 | 101 | </script> |
0 commit comments