|
| 1 | +<template> |
| 2 | + <AuthForm |
| 3 | + v-if="!emailSentSuccess && !$route.query.uid" |
| 4 | + v-slot="{ invalid }" |
| 5 | + error-header="Error sending reset link, please fix the following errors:" |
| 6 | + :errors="errors" |
| 7 | + @submit="sendResetLink" |
| 8 | + > |
| 9 | + <div class="mt-1 mb-3"> |
| 10 | + <p class="text-center text-white"> |
| 11 | + Please enter <wbr />your registered email. |
| 12 | + </p> |
| 13 | + </div> |
| 14 | + <div class="text-white" v-if="emailLoading">Sending email...</div> |
| 15 | + <InputField |
| 16 | + :id="emailField.id" |
| 17 | + :key="emailField.index" |
| 18 | + :field-name="emailField.name" |
| 19 | + :field-type="emailField.type" |
| 20 | + :field-options="emailField.options" |
| 21 | + :rules="emailField.rules" |
| 22 | + :inputvalue.sync="emailField.value" |
| 23 | + /> |
| 24 | + <AuthSubmit :disabled="invalid">Send Reset Link</AuthSubmit> |
| 25 | + </AuthForm> |
| 26 | + |
| 27 | + <AuthForm |
| 28 | + v-else-if="emailSentSuccess" |
| 29 | + class="gap-5 text-2xl font-bold text-white place-items-center" |
| 30 | + > |
| 31 | + <h1 class="text-3xl">Reset Link Has Been Sent!</h1> |
| 32 | + <font-awesome-icon |
| 33 | + :icon="['fas', 'fa-face-smile-beam']" |
| 34 | + class="text-[10rem] my-10" |
| 35 | + /> |
| 36 | + <p class="text-center"> |
| 37 | + Please check your spam folder if you cannot find the email. |
| 38 | + </p> |
| 39 | + </AuthForm> |
| 40 | + |
| 41 | + <AuthForm |
| 42 | + v-else-if="!passwordResetSuccess" |
| 43 | + v-slot="{ invalid }" |
| 44 | + error-header="Error resetting password, please fix the following errors:" |
| 45 | + :errors="errors" |
| 46 | + @submit="resetPassword" |
| 47 | + > |
| 48 | + <div class="mt-1 mb-3 text-white"> |
| 49 | + <p v-if="uid !== ''" class="text-center"> |
| 50 | + Resetting password for {{ uid }} |
| 51 | + </p> |
| 52 | + <p class="text-center">Please enter <wbr />a new password.</p> |
| 53 | + </div> |
| 54 | + <InputField |
| 55 | + v-for="field in fields" |
| 56 | + :id="field.id" |
| 57 | + :key="field.index" |
| 58 | + :field-name="field.name" |
| 59 | + :field-type="field.type" |
| 60 | + :field-options="field.options" |
| 61 | + :rules="field.rules" |
| 62 | + :inputvalue.sync="field.value" |
| 63 | + /> |
| 64 | + <AuthSubmit :disabled="invalid">Reset Password</AuthSubmit> |
| 65 | + </AuthForm> |
| 66 | + |
| 67 | + <AuthForm |
| 68 | + v-else |
| 69 | + class="gap-5 text-2xl font-bold text-white place-items-center" |
| 70 | + > |
| 71 | + <h1 class="text-3xl text-center">Congratulations!</h1> |
| 72 | + <p class="text-center"> |
| 73 | + Your password has been reset. Now you <wbr />can login with your new |
| 74 | + password! |
| 75 | + </p> |
| 76 | + <font-awesome-icon |
| 77 | + :icon="['fas', 'fa-face-smile-beam']" |
| 78 | + class="text-[10rem] my-10" |
| 79 | + /> |
| 80 | + <p> |
| 81 | + Please proceed to the |
| 82 | + <NuxtLink to="/login" class="text-blue">login page</NuxtLink> to login! |
| 83 | + </p> |
| 84 | + </AuthForm> |
| 85 | +</template> |
| 86 | + |
| 87 | +<script> |
| 88 | +let count = 0; |
| 89 | +
|
| 90 | +export default { |
| 91 | + name: 'ResetPassword', |
| 92 | + auth: false, |
| 93 | + layout: 'auth', |
| 94 | +
|
| 95 | + data: () => ({ |
| 96 | + title: 'Reset Password', |
| 97 | + passwordResetSuccess: false, |
| 98 | + emailLoading: false, |
| 99 | + emailSentSuccess: false, |
| 100 | + uid: '', |
| 101 | + errors: [], |
| 102 | +
|
| 103 | + emailField: { |
| 104 | + name: 'Email', |
| 105 | + type: 'email', |
| 106 | + index: 0, |
| 107 | + id: 'email', |
| 108 | + rules: 'required|email', |
| 109 | + }, |
| 110 | +
|
| 111 | + fields: [ |
| 112 | + { |
| 113 | + name: 'Password', |
| 114 | + type: 'password', |
| 115 | + index: count++, |
| 116 | + id: 'password', |
| 117 | + rules: 'required|min:6', |
| 118 | + }, |
| 119 | + { |
| 120 | + name: 'Confirm Password', |
| 121 | + type: 'password', |
| 122 | + index: count++, |
| 123 | + id: 'confirm', |
| 124 | + rules: 'required|password:@password', |
| 125 | + }, |
| 126 | + ], |
| 127 | + }), |
| 128 | +
|
| 129 | + head() { |
| 130 | + return { |
| 131 | + title: this.title, |
| 132 | + }; |
| 133 | + }, |
| 134 | +
|
| 135 | + async mounted() { |
| 136 | + this.resetPassword(); |
| 137 | + }, |
| 138 | +
|
| 139 | + methods: { |
| 140 | + async sendResetLink(e) { |
| 141 | + this.emailLoading = true; |
| 142 | + const elements = e.target.elements; |
| 143 | + const data = { |
| 144 | + email: elements.Email.value, |
| 145 | + }; |
| 146 | +
|
| 147 | + const url = 'auth/reset/email/'; |
| 148 | + await this.$axios |
| 149 | + .$post(url, data) |
| 150 | + .then((resp) => (this.emailSentSuccess = true)) |
| 151 | + .catch((error) => { |
| 152 | + this.errors = error.response.data; |
| 153 | + this.emailLoading = false; |
| 154 | + }); |
| 155 | + }, |
| 156 | +
|
| 157 | + async resetPassword(e = null) { |
| 158 | + const rawUid = this.$route.query.uid; |
| 159 | + const rawToken = this.$route.query.token; |
| 160 | +
|
| 161 | + if (!rawUid) { |
| 162 | + return; |
| 163 | + } |
| 164 | +
|
| 165 | + const formattedUid = |
| 166 | + rawUid.charAt(rawUid.length - 1) === '/' |
| 167 | + ? rawUid.substring(0, rawUid.length - 1) |
| 168 | + : rawUid; |
| 169 | +
|
| 170 | + const formattedToken = |
| 171 | + rawToken.charAt(rawToken.lengthrawToken - 1) === '/' |
| 172 | + ? rawToken.substring(0, rawToken.length - 1) |
| 173 | + : rawToken; |
| 174 | +
|
| 175 | + const url = |
| 176 | + 'auth/reset/' + `?token=${formattedToken}&uid=${formattedUid}/`; |
| 177 | +
|
| 178 | + try { |
| 179 | + this.uid = atob(formattedUid); |
| 180 | + } catch (err) { |
| 181 | + this.errors = { Errors: ['email UID is invalid'] }; |
| 182 | + return; |
| 183 | + } |
| 184 | +
|
| 185 | + if (e !== null) { |
| 186 | + const elements = e.target.elements; |
| 187 | + const data = { |
| 188 | + password: elements.Password.value, |
| 189 | + }; |
| 190 | + await this.$axios |
| 191 | + .$put(url, data) |
| 192 | + .then((resp) => (this.passwordResetSuccess = true)) |
| 193 | + .catch((error) => { |
| 194 | + this.errors = error.response.data; |
| 195 | + console.log(error.response.data); |
| 196 | + }); |
| 197 | + } else { |
| 198 | + await this.$axios.$get(url).catch((error) => { |
| 199 | + this.errors = error.response.data; |
| 200 | + console.log(error.response.data); |
| 201 | + }); |
| 202 | + } |
| 203 | + }, |
| 204 | + }, |
| 205 | +}; |
| 206 | +</script> |
0 commit comments