|
| 1 | +<template> |
| 2 | + |
| 3 | + <div class="w-full"> |
| 4 | + <div class="lg:w-1/2 w-full mx-auto mt-8 overflow-auto"> |
| 5 | + |
| 6 | + <div class="lg:w-2/3 w-full mx-auto mt-8"> |
| 7 | + <div class="flex flex-wrap -mx-2 mt-8"> |
| 8 | + |
| 9 | + <!-- Start getting user information--> |
| 10 | + <form @submit.prevent="register"> |
| 11 | + <div class="p-2 w-full"> |
| 12 | + <div class="relative"> |
| 13 | + <label for="name" class="leading-7 text-sm text-gray-600">Name</label> |
| 14 | + <input |
| 15 | + type="text" |
| 16 | + id="name" |
| 17 | + name="name" |
| 18 | + class="w-full bg-gray-100 rounded border border-gray-300 focus:border-indigo-500 text-base outline-none text-gray-700 py-1 px-3 leading-8 transition-colors duration-200 ease-in-out" |
| 19 | + v-model="form.name" |
| 20 | + :disabled="this.$store.state.loading" |
| 21 | + > |
| 22 | + </div> |
| 23 | + </div> |
| 24 | + <div class="p-2 w-full"> |
| 25 | + <div class="relative"> |
| 26 | + <label for="email" class="leading-7 text-sm text-gray-600">Email</label> |
| 27 | + <input |
| 28 | + type="email" |
| 29 | + id="email" |
| 30 | + name="email" |
| 31 | + class="w-full bg-gray-100 rounded border border-gray-300 focus:border-indigo-500 text-base outline-none text-gray-700 py-1 px-3 leading-8 transition-colors duration-200 ease-in-out" |
| 32 | + v-model="form.email" |
| 33 | + :disabled="this.$store.state.loading" |
| 34 | + > |
| 35 | + </div> |
| 36 | + </div> |
| 37 | + <div class="p-2 w-full"> |
| 38 | + <div class="relative"> |
| 39 | + <label for="password" class="leading-7 text-sm text-gray-600">Password</label> |
| 40 | + <input |
| 41 | + type="password" |
| 42 | + id="password" |
| 43 | + name="password" |
| 44 | + class="w-full bg-gray-100 rounded border border-gray-300 focus:border-indigo-500 text-base outline-none text-gray-700 py-1 px-3 leading-8 transition-colors duration-200 ease-in-out" |
| 45 | + v-model="form.password" |
| 46 | + :disabled="this.$store.state.loading" |
| 47 | + > |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + |
| 51 | + <div class="p-2 w-full"> |
| 52 | + <button |
| 53 | + type="submit" |
| 54 | + class="flex mx-auto text-white bg-indigo-500 border-0 py-2 px-8 focus:outline-none hover:bg-indigo-600 rounded text-lg" |
| 55 | + :disabled="this.$store.state.loading" |
| 56 | + v-text="this.$store.state.loading ? 'Loading...' : 'Regsiter'" |
| 57 | + ></button> |
| 58 | + </div> |
| 59 | + </form> |
| 60 | + |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + |
| 66 | +</template> |
| 67 | + |
| 68 | +<script> |
| 69 | +import {register} from '../../helpers/auth' |
| 70 | +import {login} from '../../helpers/auth' |
| 71 | +import validate from 'validate.js'; |
| 72 | +
|
| 73 | +export default { |
| 74 | + name: "register", |
| 75 | +
|
| 76 | + data() { |
| 77 | + return { |
| 78 | + form: { |
| 79 | + name: '', |
| 80 | + email: '', |
| 81 | + password: '' |
| 82 | + }, |
| 83 | + errors: null, |
| 84 | + }; |
| 85 | + }, |
| 86 | +
|
| 87 | + computed: { |
| 88 | + authError() { |
| 89 | + return this.$store.getters.authError; |
| 90 | + } |
| 91 | + }, |
| 92 | +
|
| 93 | + methods: { |
| 94 | + register() { |
| 95 | + const constraints = this.getConstraints(); |
| 96 | +
|
| 97 | + const errors = validate(this.$data.form, constraints) |
| 98 | +
|
| 99 | + if (errors) { |
| 100 | + this.errors = errors; |
| 101 | + this.notifyErrors(this.errors); |
| 102 | + return; |
| 103 | + } |
| 104 | +
|
| 105 | + register(this.$data.form) |
| 106 | + .then((res) => { |
| 107 | +
|
| 108 | + //login user after registration completed |
| 109 | + this.$store.dispatch('login'); |
| 110 | +
|
| 111 | + login(this.$data.form) |
| 112 | + .then((res) => { |
| 113 | + this.$store.commit("loginSuccess", res); |
| 114 | + this.$router.push({path: '/'}); |
| 115 | + this.$toaster.info('Registration successfully done, now you are logged in :))') |
| 116 | + }) |
| 117 | + .catch((error) => { |
| 118 | + this.$store.commit("loginFailed", {error}); |
| 119 | + this.$toaster.error('Registration failed :(('); |
| 120 | + }); |
| 121 | +
|
| 122 | + }) |
| 123 | + .catch((error) => { |
| 124 | +
|
| 125 | + console.log("error error") |
| 126 | +
|
| 127 | + }); |
| 128 | + }, |
| 129 | +
|
| 130 | + getConstraints() { |
| 131 | + return { |
| 132 | + name: { |
| 133 | + presence: true, |
| 134 | + length: { |
| 135 | + minimum: 3, |
| 136 | + message: 'Must be at least 3 characters long' |
| 137 | + } |
| 138 | + }, |
| 139 | + email: { |
| 140 | + presence: true, |
| 141 | + email: true, |
| 142 | + }, |
| 143 | + password: { |
| 144 | + presence: true, |
| 145 | + length: { |
| 146 | + minimum: 8, |
| 147 | + message: 'Must be at least 8 characters long' |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + }, |
| 152 | +
|
| 153 | + notifyErrors(errors) { |
| 154 | + this.$toaster.error(errors); |
| 155 | + }, |
| 156 | + }, |
| 157 | +} |
| 158 | +</script> |
| 159 | + |
| 160 | +<style scoped> |
| 161 | +
|
| 162 | +</style> |
0 commit comments