ResetPasswordRequestDialog.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <Form
  3. class="flex flex-col gap-2 p-5"
  4. v-slot="$form"
  5. :initialValues="initialValues"
  6. :resolver="resolver"
  7. :validateOnValueUpdate="false"
  8. :validateOnBlur="true"
  9. @submit="onFormSubmit"
  10. >
  11. <div class="flex flex-col">
  12. <label
  13. for="email"
  14. class="ml-1 text-sm text-primary dark:text-primary"
  15. >
  16. Email Address
  17. </label>
  18. <InputText
  19. name="email"
  20. fluid
  21. id="email"
  22. type="email"
  23. placeholder="Email address"
  24. autocomplete="email"
  25. />
  26. <Message
  27. v-if="$form.email?.invalid"
  28. severity="error"
  29. size="small"
  30. variant="simple"
  31. >
  32. {{ $form.email?.error?.message }}
  33. </Message>
  34. </div>
  35. <Button
  36. label="Request Password Reset"
  37. type="submit"
  38. fluid
  39. :disabled="!$form.valid"
  40. :loading="isLoading"
  41. class="mt-5"
  42. >
  43. <template #loadingicon>
  44. <Icon
  45. class="animate-spin"
  46. name="ph:circle-notch-bold"
  47. />
  48. </template>
  49. </Button>
  50. </Form>
  51. </template>
  52. <script setup>
  53. import { zodResolver } from "@primeuix/forms/resolvers/zod"
  54. const dialogRef = inject("dialogRef")
  55. const toast = useToast()
  56. const { authClient } = useAuthClient()
  57. const resolver = ref(
  58. zodResolver(
  59. z.object({
  60. email: z.email("Not an email address.")
  61. })
  62. )
  63. )
  64. const initialValues = ref({ email: "" })
  65. const isLoading = ref(false)
  66. async function onFormSubmit({ valid, values }) {
  67. if (valid) {
  68. await resetPasswordRequest(values.email)
  69. closeDialog()
  70. }
  71. }
  72. async function resetPasswordRequest(email) {
  73. if (isLoading.value) {
  74. return
  75. }
  76. isLoading.value = true
  77. // noinspection JSUnresolvedReference
  78. const { error } = await authClient.requestPasswordReset({
  79. email: email,
  80. redirectTo: { name: "reset-password" }
  81. })
  82. isLoading.value = false
  83. if (error) {
  84. console.error(error)
  85. toast.add({
  86. severity: "error",
  87. summary: "Request Password Reset Error.",
  88. detail: error.data?.message || error.message
  89. })
  90. } else {
  91. toast.add({
  92. severity: "success",
  93. summary: "Password Reset Requested.",
  94. detail: "You will receive an email with a link to reset your password.",
  95. life: 3000
  96. })
  97. }
  98. }
  99. function closeDialog() {
  100. dialogRef.value.close()
  101. }
  102. </script>
  103. <style scoped></style>