Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cb6e143

Browse filesBrowse files
committed
feature(login) allow login via username or password
1 parent 6772895 commit cb6e143
Copy full SHA for cb6e143

File tree

Expand file treeCollapse file tree

4 files changed

+22
-16
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+22
-16
lines changed

‎composables/useAuth.ts

Copy file name to clipboardExpand all lines: composables/useAuth.ts
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export async function registerWithEmail(
7272
}
7373
}
7474

75-
export async function loginWithEmail(email: string, password: string) {
76-
const user = await $fetch<IUser>('/api/auth/login', { method: 'POST', body: { email: email, password: password } })
75+
export async function loginWithEmail(usernameOrEmail: string, password: string) {
76+
const user = await $fetch<IUser>('/api/auth/login', { method: 'POST', body: { usernameOrEmail: usernameOrEmail, password: password } })
7777
useState('user').value = user
7878
await useRouter().push('/dashboard')
7979
}

‎pages/login.vue

Copy file name to clipboardExpand all lines: pages/login.vue
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { ref } from "@vue/reactivity";
33
import {loginWithEmail} from "~/composables/useAuth";
44
5-
const email = ref(null)
5+
const usernameOrEmail = ref(null)
66
const password = ref(null)
77
const hasError = ref(null)
88
const errorMessage = ref(null)
@@ -13,7 +13,7 @@ definePageMeta({
1313
})
1414
1515
const postLoginForm = async function () {
16-
await loginWithEmail(email.value, password.value)
16+
await loginWithEmail(usernameOrEmail.value, password.value)
1717
}
1818
</script>
1919

@@ -50,10 +50,10 @@ const postLoginForm = async function () {
5050
<input type="hidden" name="remember" value="true">
5151
<div class="rounded-md shadow-sm -space-y-px mb-1">
5252
<div>
53-
<label for="email-address" class="sr-only">Email address</label>
54-
<input v-model="email" id="email-address" name="email" type="email" autocomplete="email" required
53+
<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
5555
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"
56-
placeholder="Email address">
56+
placeholder="Username or Email">
5757
</div>
5858
</div>
5959
<div>

‎server/api/auth/login.ts

Copy file name to clipboardExpand all lines: server/api/auth/login.ts
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { makeSession } from '~~/server/services/sessionService';
66

77
export default async (event: CompatibilityEvent) => {
88
const body = await readBody(event)
9-
const email: string = body.email
9+
const usernameOrEmail: string = body.usernameOrEmail
1010
const password: string = body.password
11-
const user = await getUserByEmail(email)
11+
12+
const user = await getUserByEmail(usernameOrEmail)
1213

1314
if (user === null) {
1415
sendError(event, createError({ statusCode: 401, statusMessage: 'Unauthenticated' }))

‎server/database/repositories/userRespository.ts

Copy file name to clipboardExpand all lines: server/database/repositories/userRespository.ts
+12-7Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import prisma from "~/server/database/client";
2-
import {IUser} from '~/types/IUser';
2+
import { IUser } from '~/types/IUser';
33
import { ISubscription } from "~~/types/ISubscription";
44

5-
export async function getUserByEmail(email: string): Promise<IUser> {
6-
return await prisma.user.findUnique({
5+
export async function getUserByEmail(emailOrEmail: string): Promise<IUser> {
6+
return await prisma.user.findFirst({
77
where: {
8-
email: email,
8+
OR:
9+
[
10+
{ email: emailOrEmail },
11+
{ username: emailOrEmail },
12+
]
913
},
1014
select: {
1115
id: true,
1216
username: true,
17+
password: true
1318
},
1419
})
1520
}
@@ -78,7 +83,7 @@ export async function getSubscriptionById(stripeId: string): Promise<ISubscripti
7883

7984
export async function updateStripeCustomerId(data: IUser) {
8085
return await prisma.user.update({
81-
where: {email: data.email},
86+
where: { email: data.email },
8287
data: {
8388
stripeCustomerId: data.stripeCustomerId,
8489
}
@@ -87,8 +92,8 @@ export async function updateStripeCustomerId(data: IUser) {
8792

8893
export async function createOrUpdateSubscription(data: ISubscription) {
8994
return await prisma.subscription.upsert({
90-
where:{
91-
stripeId: data.stripeId
95+
where: {
96+
stripeId: data.stripeId
9297
},
9398
create: {
9499
userId: data.userId,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.