Skip to content

Commit dbc922f

Browse files
committed
feature(login) show invalid credentials feedback
1 parent 72a7077 commit dbc922f

File tree

3 files changed

+33
-52
lines changed

3 files changed

+33
-52
lines changed

components/layout/navbar.vue

+1-38
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ watch(user, async () => {
2626
await checkIfLoggedIn()
2727
}, { deep: true });
2828
29-
30-
3129
</script>
3230

3331
<template>
@@ -53,12 +51,8 @@ watch(user, async () => {
5351
src="/img/logo_clear_fsj.png" />
5452
</nuxt-link>
5553
</div>
56-
5754
</div>
58-
59-
6055
<nav class="hidden md:flex justify-between space-x-10 align-bottom mt-14">
61-
6256
<div class="hidden md:flex space-x-10 align-bottom">
6357
<nuxt-link to="/subscribe">
6458
<span
@@ -127,11 +121,6 @@ watch(user, async () => {
127121
</svg>
128122
</NuxtLink>
129123

130-
131-
132-
133-
134-
135124
<span class="hidden md:block " @click="setColorTheme($colorMode.preference == 'dark' ? 'light' : 'dark')">
136125
<svg v-if="$colorMode.value == 'dark'" xmlns="http://www.w3.org/2000/svg"
137126
class="h-6 w-6 text-gray-50 hidden lg:block" viewBox="0 0 20 20" fill="currentColor">
@@ -146,27 +135,8 @@ watch(user, async () => {
146135
</span>
147136

148137
<User :isLoggedIn="isLoggedIn" class="hidden md:block" />
149-
150-
<span class="hidden md:flex items-center justify-end md:flex-1 lg:w-0">
151-
<!-- <nuxt-link v-if="!user" to="/login"
152-
class="whitespace-nowrap text-base font-medium text-gray-500 dark:text-gray-200 hover:text-gray-900 dark:hover:text-gray-300 "> Sign
153-
in
154-
</nuxt-link> -->
155-
156-
<!-- <User v-if="user" :user="user" /> -->
157-
158-
<!-- <nuxt-link v-if="!user" to="/register"
159-
class="transition duration-500 hover:scale-110 ml-8 whitespace-nowrap inline-flex items-center justify-center px-4 py-2 border border-transparent rounded-md shadow-sm text-base font-medium text-white bg-indigo-600 hover:bg-indigo-700">
160-
Sign up
161-
</nuxt-link> -->
162-
</span>
163138
</div>
164-
165-
166-
167-
168139
</div>
169-
<!-- <BackButton /> -->
170140
</div>
171141

172142

@@ -190,9 +160,7 @@ watch(user, async () => {
190160
</button>
191161

192162
<div class="py-4 overflow-y-auto">
193-
194163
<ul class="space-y-2">
195-
196164
<li>
197165

198166
<button class="ml-2" @click="setColorTheme($colorMode.preference == 'dark' ? 'light' : 'dark')">
@@ -208,8 +176,6 @@ watch(user, async () => {
208176
</svg>
209177
</button>
210178
</li>
211-
212-
213179
<NuxtLink to="https://github.com/jurassicjs/nuxt3-fullstack-tutorial">
214180
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true"
215181
role="img" class="text-gray-500 dark:text-gray-200 h-6 w-6 ml-2 mt-2 " width="0.97em" height="1em"
@@ -219,7 +185,6 @@ watch(user, async () => {
219185
</path>
220186
</svg>
221187
</NuxtLink>
222-
223188
<NuxtLink to="https://discord.gg/tFGTQBdT" class="text-gray-800">
224189
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
225190
class="text-gray-500 dark:text-gray-200 h-6 w-6 ml-2 mt-2" viewBox="0 -28.5 256 256" version="1.1"
@@ -231,15 +196,13 @@ watch(user, async () => {
231196
</g>
232197
</svg>
233198
</NuxtLink>
234-
235199
<NuxtLink to="https://twitter.com/jack_fullstack">
236200
<svg class="text-gray-500 dark:text-gray-200 h-6 w-6 ml-2 mt-2" fill="none" viewBox="0 0 24 24">
237201
<path fill-rule="evenodd" clip-rule="evenodd"
238202
d="M22 5.897c-.75.33-1.5.577-2.333.66A4.4 4.4 0 0021.5 4.33c-.833.495-1.667.825-2.583.99a4.053 4.053 0 00-3-1.32c-2.25 0-4.084 1.814-4.084 4.041 0 .33 0 .66.084.907-3.5-.165-6.5-1.814-8.5-4.288-.417.66-.584 1.32-.584 2.062 0 1.402.75 2.639 1.834 3.381-.667 0-1.334-.165-1.834-.495v.083c0 1.98 1.417 3.629 3.25 3.958-.333.083-.666.165-1.083.165-.25 0-.5 0-.75-.082.5 1.65 2 2.804 3.833 2.804C6.667 17.608 4.917 18.268 3 18.268c-.333 0-.667 0-1-.082C3.833 19.34 6 20 8.25 20c7.583 0 11.667-6.186 11.667-11.546v-.495c.833-.578 1.5-1.32 2.083-2.062z"
239203
fill="currentColor"></path>
240204
</svg>
241205
</NuxtLink>
242-
243206
<li>
244207
<NuxtLink to="/dashboard"
245208
class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700">
@@ -303,7 +266,7 @@ watch(user, async () => {
303266
<span class="flex-1 ml-3 whitespace-nowrap">Ask Jack</span>
304267
</NuxtLink>
305268
</li>
306-
<li v-if="!isLoggedIn">
269+
<li v-if="!isLoggedIn">
307270
<NuxtLink to="/login"
308271
class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700">
309272
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"

composables/useAuth.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,16 @@ export async function registerWithEmail(
7878
}
7979
}
8080

81-
export async function loginWithEmail(usernameOrEmail: string, password: string) {
82-
const user = await $fetch<IUser>('/api/auth/login', { method: 'POST', body: { usernameOrEmail: usernameOrEmail, password: password } })
83-
useState('user').value = user
84-
await useRouter().push('/dashboard')
81+
export async function loginWithEmail(usernameOrEmail: string, password: string): Promise<boolean> {
82+
83+
try{
84+
const user = await $fetch<IUser>('/api/auth/login', { method: 'POST', body: { usernameOrEmail: usernameOrEmail, password: password } })
85+
console.log(user)
86+
useState('user').value = user
87+
await useRouter().push('/dashboard')
88+
return true
89+
} catch(e) {
90+
return false
91+
}
92+
8593
}

pages/login.vue

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { ref } from "@vue/reactivity";
3-
import {loginWithEmail} from "~/composables/useAuth";
3+
import { loginWithEmail } from "~/composables/useAuth";
44
55
const usernameOrEmail = ref(null)
66
const password = ref(null)
@@ -13,24 +13,32 @@ definePageMeta({
1313
})
1414
1515
const postLoginForm = async function () {
16-
await loginWithEmail(usernameOrEmail.value, password.value)
16+
const res = await loginWithEmail(usernameOrEmail.value, password.value)
17+
if (!res) {
18+
errorMessage.value = 'Invalid Credentials'
19+
hasError.value = true
20+
setTimeout(() => {
21+
hasError.value = false
22+
}, 3000)
23+
}
1724
}
1825
</script>
1926

2027
<template>
21-
<div class="h-screen bg-gradient-to-b from-white to-blue-200 dark:bg-gradient-to-b dark:from-slate-800 dark:to-slate-400">
28+
<div
29+
class="h-screen bg-gradient-to-b from-white to-blue-200 dark:bg-gradient-to-b dark:from-slate-800 dark:to-slate-400">
2230
<div class="flex items-center justify-center px-4 sm:px-6 lg:px-8">
2331
<div class="max-w-md w-full space-y-8">
2432
<div>
2533
<div class="h-25 w-25">
2634
</div>
2735
<div class="lg:flex mt-10">
28-
<img class="mx-auto h-24 w-auto" src="/img/logo_clear_fsj.png" alt="full stack jack logo" />
29-
<br>
30-
<h1 class="py-9 text-center text-5xl font-extrabold text-gray-900 dark:text-gray-400">
31-
Full Stack Jack</h1>
36+
<img class="mx-auto h-24 w-auto" src="/img/logo_clear_fsj.png" alt="full stack jack logo" />
37+
<br>
38+
<h1 class="py-9 text-center text-5xl font-extrabold text-gray-900 dark:text-gray-400">
39+
Full Stack Jack</h1>
3240
</div>
33-
41+
3442
<h2 class="mt-6 py-9 text-center text-3xl font-extrabold text-gray-900 dark:text-gray-400">Sign in</h2>
3543
</div>
3644

@@ -51,7 +59,8 @@ const postLoginForm = async function () {
5159
<div class="rounded-md shadow-sm -space-y-px mb-1">
5260
<div>
5361
<label for="email-address" class="sr-only">Username or Email</label>
54-
<input v-model="usernameOrEmail" id="email-address" name="email" type="email" autocomplete="email" required
62+
<input v-model="usernameOrEmail" id="email-address" name="email" type="email" autocomplete="email"
63+
required
5564
class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
5665
placeholder="Username or Email">
5766
</div>
@@ -72,7 +81,8 @@ const postLoginForm = async function () {
7281
</div>
7382

7483
<div class="text-sm">
75-
<a href="#" class="font-medium text-indigo-600 hover:text-indigo-500 dark:text-white"> Forgot your password? </a>
84+
<a href="#" class="font-medium text-indigo-600 hover:text-indigo-500 dark:text-white"> Forgot your
85+
password? </a>
7686
</div>
7787
</div>
7888

0 commit comments

Comments
 (0)