|
| 1 | +<script setup lang="ts"> |
| 2 | +const supabase = useSupabaseClient(); |
| 3 | +
|
| 4 | +const state = reactive({ |
| 5 | + email: "", |
| 6 | +}); |
| 7 | +
|
| 8 | +const loginError = ref(false); |
| 9 | +const loginSuccess = ref(false); |
| 10 | +
|
| 11 | +async function onSubmit() { |
| 12 | + const { error } = await supabase.auth.signInWithOtp({ |
| 13 | + email: state.email, |
| 14 | + options: { |
| 15 | + emailRedirectTo: "http://localhost:3000/confirm", |
| 16 | + }, |
| 17 | + }); |
| 18 | +
|
| 19 | + if (error) { |
| 20 | + loginError.value = true; |
| 21 | + loginSuccess.value = false; |
| 22 | + } else { |
| 23 | + loginSuccess.value = true; |
| 24 | + loginError.value = false; |
| 25 | + } |
| 26 | +} |
| 27 | +
|
| 28 | +const user = useSupabaseUser(); |
| 29 | +watch( |
| 30 | + user, |
| 31 | + () => { |
| 32 | + if (user.value) { |
| 33 | + return navigateTo("/"); |
| 34 | + } |
| 35 | + }, |
| 36 | + { immediate: true } |
| 37 | +); |
| 38 | +</script> |
| 39 | + |
| 40 | +<template> |
| 41 | + <UContainer :ui="{ constrained: 'max-w-xl' }"> |
| 42 | + <UCard class="mt-10"> |
| 43 | + <template #header> |
| 44 | + <div class="flex justify-between"> |
| 45 | + <h1 class="font-bold text-xl">Log in</h1> |
| 46 | + <UColorModeButton /> |
| 47 | + </div> |
| 48 | + </template> |
| 49 | + |
| 50 | + <section> |
| 51 | + <UForm |
| 52 | + v-if="!loginError && !loginSuccess" |
| 53 | + :state="state" |
| 54 | + class="space-y-4" |
| 55 | + @submit="onSubmit" |
| 56 | + > |
| 57 | + <UFormGroup label="Email" name="email" v-slot="{ error }"> |
| 58 | + <UInput |
| 59 | + v-model="state.email" |
| 60 | + type="email" |
| 61 | + placeholder="Enter email" |
| 62 | + :icon=" |
| 63 | + error |
| 64 | + ? 'i-heroicons-exclamation-triangle-20-solid' |
| 65 | + : 'i-heroicons-envelope' |
| 66 | + " |
| 67 | + /> |
| 68 | + </UFormGroup> |
| 69 | + |
| 70 | + <UButton type="submit"> Submit </UButton> |
| 71 | + </UForm> |
| 72 | + <UAlert |
| 73 | + v-if="loginError" |
| 74 | + color="red" |
| 75 | + variant="subtle" |
| 76 | + icon="i-heroicons-exclamation-triangle-20-solid" |
| 77 | + title="There was an error" |
| 78 | + description="Please reload this page to try again." |
| 79 | + /> |
| 80 | + <UAlert |
| 81 | + v-if="loginSuccess" |
| 82 | + color="green" |
| 83 | + variant="subtle" |
| 84 | + icon="i-heroicons-check-badge" |
| 85 | + title="Check your email" |
| 86 | + description="A magic login link has been send to your email address." |
| 87 | + /> |
| 88 | + </section> |
| 89 | + </UCard> |
| 90 | + </UContainer> |
| 91 | +</template> |
0 commit comments