|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref } from 'vue' |
| 3 | +import { useRouter } from 'vue-router' |
| 4 | +
|
| 5 | +const props = defineProps({ |
| 6 | + showLogo: { |
| 7 | + type: Boolean, |
| 8 | + default: true |
| 9 | + }, |
| 10 | + headingText: { |
| 11 | + type: String, |
| 12 | + default: 'Sign in to your account' |
| 13 | + }, |
| 14 | + showSocialLogin: { |
| 15 | + type: Boolean, |
| 16 | + default: true |
| 17 | + }, |
| 18 | + showRememberMe: { |
| 19 | + type: Boolean, |
| 20 | + default: true |
| 21 | + }, |
| 22 | + showForgotPassword: { |
| 23 | + type: Boolean, |
| 24 | + default: true |
| 25 | + }, |
| 26 | + showSignup: { |
| 27 | + type: Boolean, |
| 28 | + default: true |
| 29 | + }, |
| 30 | + signupText: { |
| 31 | + type: String, |
| 32 | + default: 'Start a 14 day free trial' |
| 33 | + } |
| 34 | +}) |
| 35 | +
|
| 36 | +// Reactive state for form inputs and messages |
| 37 | +const email = ref('') |
| 38 | +const password = ref('') |
| 39 | +const errorMessage = ref('') |
| 40 | +
|
| 41 | +// Router instance |
| 42 | +const router = useRouter() |
| 43 | +
|
| 44 | +// Method to handle email and password login |
| 45 | +async function login() { |
| 46 | + const body = { |
| 47 | + email: email.value, |
| 48 | + password: password.value, |
| 49 | + } |
| 50 | +
|
| 51 | + try { |
| 52 | + const url = 'http://localhost:3008/api/login' |
| 53 | +
|
| 54 | + const response = await fetch(url, { |
| 55 | + method: 'POST', |
| 56 | + headers: { |
| 57 | + 'Content-Type': 'application/json', |
| 58 | + 'Accept': 'application/json', |
| 59 | + }, |
| 60 | + body: JSON.stringify(body), |
| 61 | + }) |
| 62 | +
|
| 63 | + const data: any = await response.json() |
| 64 | +
|
| 65 | + if (!response.ok) { |
| 66 | + errorMessage.value = data.error || 'Login failed' |
| 67 | + } |
| 68 | + else { |
| 69 | + localStorage.setItem('token', data.token) |
| 70 | + router.push({ path: '/dashboard' }) |
| 71 | + } |
| 72 | + } |
| 73 | + catch (error) { |
| 74 | + console.error(error) |
| 75 | + errorMessage.value = 'An error occurred during login' |
| 76 | + } |
| 77 | + finally { |
| 78 | + // Clear password after login attempt |
| 79 | + password.value = '' |
| 80 | + } |
| 81 | +} |
| 82 | +</script> |
| 83 | + |
1 | 84 | <template>
|
2 | 85 | <div class="min-h-full flex flex-col justify-center py-12 lg:px-8 sm:px-6">
|
3 | 86 | <div class="sm:mx-auto sm:max-w-md sm:w-full">
|
| 87 | + <Logo v-if="showLogo" class="mx-auto h-11 w-auto" /> |
4 | 88 | <h2 class="mt-6 text-center text-2xl text-gray-900 font-bold leading-9 tracking-tight">
|
5 |
| - Sign in to your account |
| 89 | + {{ headingText }} |
6 | 90 | </h2>
|
7 | 91 | </div>
|
8 | 92 |
|
9 | 93 | <div class="mt-10 sm:mx-auto sm:max-w-[480px] sm:w-full">
|
10 | 94 | <div class="bg-white px-6 py-12 shadow sm:rounded-lg sm:px-12">
|
11 |
| - <form class="space-y-6" action="#" method="POST"> |
| 95 | + <div class="space-y-6"> |
12 | 96 | <div>
|
13 | 97 | <label for="email" class="block text-sm text-gray-900 font-medium leading-6">Email address</label>
|
14 | 98 | <div class="mt-2">
|
15 |
| - <input id="email" name="email" type="email" autocomplete="email" required class="block w-full border-0 rounded-md p-2 py-1.5 text-gray-900 shadow-sm ring-1 ring-gray-300 ring-inset sm:text-sm placeholder:text-gray-400 sm:leading-6 focus:ring-2 focus:ring-teal-600 focus:ring-inset"> |
| 99 | + <input id="email" v-model="email" name="email" type="email" autocomplete="email" required class="block w-full border-0 rounded-md py-1.5 text-gray-900 shadow-sm ring-1 ring-gray-300 ring-inset sm:text-sm placeholder:text-gray-400 sm:leading-6 focus:ring-2 focus:ring-indigo-600 focus:ring-inset"> |
16 | 100 | </div>
|
17 | 101 | </div>
|
18 | 102 |
|
19 | 103 | <div>
|
20 | 104 | <label for="password" class="block text-sm text-gray-900 font-medium leading-6">Password</label>
|
21 | 105 | <div class="mt-2">
|
22 |
| - <input id="password" name="password" type="password" autocomplete="current-password" required class="block w-full border-0 rounded-md p-2 py-1.5 text-gray-900 shadow-sm ring-1 ring-gray-300 ring-inset sm:text-sm placeholder:text-gray-400 sm:leading-6 focus:ring-2 focus:ring-teal-600 focus:ring-inset"> |
| 106 | + <input id="password" v-model="password" name="password" type="password" autocomplete="current-password" required class="block w-full border-0 rounded-md py-1.5 text-gray-900 shadow-sm ring-1 ring-gray-300 ring-inset sm:text-sm placeholder:text-gray-400 sm:leading-6 focus:ring-2 focus:ring-indigo-600 focus:ring-inset"> |
23 | 107 | </div>
|
24 | 108 | </div>
|
25 | 109 |
|
26 | 110 | <div class="flex items-center justify-between">
|
27 |
| - <div class="flex items-center"> |
28 |
| - <input id="remember-me" name="remember-me" type="checkbox" class="h-4 w-4 border-gray-300 rounded text-teal-600 focus:ring-teal-600"> |
| 111 | + <div v-if="showRememberMe" class="flex items-center"> |
| 112 | + <input id="remember-me" name="remember-me" type="checkbox" class="h-4 w-4 border-gray-300 rounded text-indigo-600 focus:ring-indigo-600"> |
29 | 113 | <label for="remember-me" class="ml-3 block text-sm text-gray-900 leading-6">Remember me</label>
|
30 | 114 | </div>
|
31 | 115 |
|
32 |
| - <div class="text-sm leading-6"> |
33 |
| - <a href="#" class="text-teal-600 font-semibold hover:text-teal-500">Forgot password?</a> |
| 116 | + <div v-if="showForgotPassword" class="text-sm leading-6"> |
| 117 | + <a href="#" class="text-indigo-600 font-semibold hover:text-indigo-500">Forgot password?</a> |
34 | 118 | </div>
|
35 | 119 | </div>
|
36 | 120 |
|
37 | 121 | <div>
|
38 |
| - <button type="submit" class="w-full flex justify-center rounded-md bg-teal-600 px-3 py-1.5 text-sm text-white font-semibold leading-6 shadow-sm hover:bg-teal-500 focus-visible:outline-2 focus-visible:outline-teal-600 focus-visible:outline-offset-2 focus-visible:outline"> |
| 122 | + <button type="submit" class="w-full flex justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm text-white font-semibold leading-6 shadow-sm hover:bg-indigo-500 focus-visible:outline-2 focus-visible:outline-indigo-600 focus-visible:outline-offset-2 focus-visible:outline" @click="login"> |
39 | 123 | Sign in
|
40 | 124 | </button>
|
41 | 125 | </div>
|
42 |
| - </form> |
| 126 | + </div> |
43 | 127 |
|
44 |
| - <div> |
| 128 | + <div v-if="showSocialLogin"> |
45 | 129 | <div class="relative mt-10">
|
46 | 130 | <div class="absolute inset-0 flex items-center" aria-hidden="true">
|
47 | 131 | <div class="w-full border-t border-gray-200" />
|
|
72 | 156 | </div>
|
73 | 157 | </div>
|
74 | 158 |
|
75 |
| - <p class="mt-10 text-center text-sm text-gray-500"> |
| 159 | + <p v-if="showSignup" class="mt-10 text-center text-sm text-gray-500"> |
76 | 160 | Not a member?
|
77 |
| - <a href="#" class="text-teal-600 font-semibold leading-6 hover:text-teal-500">Start a 14 day free trial</a> |
| 161 | + <a href="#" class="text-indigo-600 font-semibold leading-6 hover:text-indigo-500">Start a 14 day free trial</a> |
78 | 162 | </p>
|
79 | 163 | </div>
|
80 | 164 | </div>
|
|
0 commit comments