diff options
| author | Zephyrrus <[email protected]> | 2020-07-02 02:50:55 +0300 |
|---|---|---|
| committer | Zephyrrus <[email protected]> | 2020-07-02 02:50:55 +0300 |
| commit | 720ffaf0083564c85a07d66a6d303f34706add41 (patch) | |
| tree | 033405206c4dd981b32a76ba5033bec3692e8610 /src/site/pages/login.vue | |
| parent | chore: add compound launcher and basic prettier (diff) | |
| download | host.fuwn.me-720ffaf0083564c85a07d66a6d303f34706add41.tar.xz host.fuwn.me-720ffaf0083564c85a07d66a6d303f34706add41.zip | |
feat: start refactoring the code to actually use vuex
This includes creating multiple stores as needed for components and removing all complex states from components (since all those states should be stored in vuex)
Diffstat (limited to 'src/site/pages/login.vue')
| -rw-r--r-- | src/site/pages/login.vue | 39 |
1 files changed, 16 insertions, 23 deletions
diff --git a/src/site/pages/login.vue b/src/site/pages/login.vue index 514cbc5..3c43755 100644 --- a/src/site/pages/login.vue +++ b/src/site/pages/login.vue @@ -63,6 +63,8 @@ </template> <script> +import { mapState } from 'vuex'; + export default { name: 'Login', data() { @@ -74,40 +76,31 @@ export default { isLoading: false }; }, - computed: { - config() { - return this.$store.state.config; - } - }, + computed: mapState(['config', 'auth']), metaInfo() { return { title: 'Login' }; }, + created() { + if (this.auth.loggedIn) { + this.redirect(); + } + }, methods: { async login() { - if (this.isLoading) return; - if (!this.username || !this.password) { - this.$store.dispatch('alert', { + if (this.auth.isLoading) return; + + const { username, password } = this; + if (!username || !password) { + this.$store.dispatch('alert/set', { text: 'Please fill both fields before attempting to log in.', error: true }); return; } - this.isLoading = true; - - try { - const data = await this.$axios.$post(`auth/login`, { - username: this.username, - password: this.password - }); - this.$axios.setToken(data.token, 'Bearer'); - document.cookie = `token=${encodeURIComponent(data.token)}`; - this.$store.dispatch('login', { token: data.token, user: data.user }); + await this.$store.dispatch('auth/login', { username, password }); + if (this.auth.loggedIn) { this.redirect(); - } catch (error) { - // - } finally { - this.isLoading = false; } }, /* @@ -126,7 +119,7 @@ export default { }); },*/ redirect() { - this.$store.commit('loggedIn', true); + console.log('redirect'); if (typeof this.$route.query.redirect !== 'undefined') { this.$router.push(this.$route.query.redirect); return; |