|
| 1 | +<template> |
| 2 | + <page title="Sign Up"> |
| 3 | + <error-alert :watch="error"></error-alert> |
| 4 | + |
| 5 | + <form id="new-user" class="needs-validation mt-3" novalidate> |
| 6 | + <div class="form-group"> |
| 7 | + <label for="email">Email:</label> |
| 8 | + <input v-model="email" type="email" class="form-control" id="email" placeholder="Enter email" required/> |
| 9 | + <div class="invalid-feedback"> |
| 10 | + Enter a valid email address |
| 11 | + </div> |
| 12 | + </div> |
| 13 | + <div class="form-group"> |
| 14 | + <label for="password">Password:</label> |
| 15 | + <input v-model="password" type="password" class="form-control" id="password" placeholder="Password" required/> |
| 16 | + <div class="invalid-feedback"> |
| 17 | + Password is required |
| 18 | + </div> |
| 19 | + </div> |
| 20 | + <div class="form-group"> |
| 21 | + <label for="password-confirm">Re-enter Password:</label> |
| 22 | + <input v-model="passwordConfirm" type="password" class="form-control" id="password-confirm" placeholder="Password" required/> |
| 23 | + <div class="invalid-feedback"> |
| 24 | + You must enter the same password again |
| 25 | + </div> |
| 26 | + </div> |
| 27 | + <button type="submit" class="btn btn-primary" @click.prevent="createUser"> |
| 28 | + <span v-if="submitting" class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> |
| 29 | + {{ submitText }} |
| 30 | + </button> |
| 31 | + </form> |
| 32 | + </page> |
| 33 | +</template> |
| 34 | + |
| 35 | +<script> |
| 36 | +import ErrorAlert from '@/components/utilities/Error'; |
| 37 | +import Page from '@/components/page/Page'; |
| 38 | +
|
| 39 | +export default { |
| 40 | + components: { Page, ErrorAlert }, |
| 41 | + data: function () { |
| 42 | + return { |
| 43 | + error: null, |
| 44 | + submitting: false, |
| 45 | + submitText: 'Submit', |
| 46 | +
|
| 47 | + email: null, |
| 48 | + password: null, |
| 49 | + passwordConfirm: null |
| 50 | + }; |
| 51 | + }, |
| 52 | + methods: { |
| 53 | + createUser: function () { |
| 54 | + if (!this.validateFields()) { return; } |
| 55 | + this.submitting = true; |
| 56 | + this.submitText = 'Submitting'; |
| 57 | +
|
| 58 | + var auth = { |
| 59 | + email: this.email, |
| 60 | + password: this.password |
| 61 | + }; |
| 62 | +
|
| 63 | + this.$api.users.post('/register', auth).then((response) => { |
| 64 | + this.submitText = 'Logging In'; |
| 65 | + return this.$api.users.post('/login', auth); |
| 66 | + }).then((response) => { |
| 67 | + this.submitting = false; |
| 68 | + this.submitText = 'Success!'; |
| 69 | +
|
| 70 | + this.$store.commit('auth/token', response.data.accessToken); |
| 71 | + this.$store.commit('auth/refresh', response.data.refreshToken); |
| 72 | + this.$store.commit('auth/authenticated', true); |
| 73 | +
|
| 74 | + this.$router.push({ path: this.$route.query.redirect || '/' }); |
| 75 | + }).catch((error) => { |
| 76 | + if (error.response && error.response.data && error.response.data.error) { |
| 77 | + this.error = new Error(error.response.data.error); |
| 78 | + } else { |
| 79 | + this.error = error; |
| 80 | + } |
| 81 | +
|
| 82 | + this.submitting = false; |
| 83 | + }); |
| 84 | + }, |
| 85 | + validateFields: function () { |
| 86 | + var form = document.getElementById('new-user'); |
| 87 | + form.classList.add('was-validated'); |
| 88 | +
|
| 89 | + if (this.password !== this.passwordConfirm) { |
| 90 | + document.getElementById('password-confirm').classList.add('is-invalid'); |
| 91 | + return false; |
| 92 | + } else { |
| 93 | + document.getElementById('password-confirm').classList.remove('is-invalid'); |
| 94 | + } |
| 95 | +
|
| 96 | + return form.checkValidity(); |
| 97 | + } |
| 98 | + } |
| 99 | +}; |
| 100 | +</script> |
0 commit comments