-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement login & logout logic (#23)
- Loading branch information
Showing
9 changed files
with
61 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default { | ||
extends: ['@commitlint/config-conventional'] | ||
} | ||
extends: ['@commitlint/config-conventional'], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,17 +11,26 @@ const { $client } = useNuxtApp() | |
const route = useRoute() | ||
const toast = useToast() | ||
// Check if user is logged in | ||
onMounted(() => { | ||
const { user } = useSessionUser() | ||
if (user.value) { | ||
// TODO: Wrap a utils function | ||
const r = route.query.r as string | ||
navigateTo(decodeURIComponent(r || '/dashboard')) | ||
} | ||
}) | ||
// Form | ||
const schema = z.object({ | ||
email: z.string(), | ||
password: z.string(), | ||
}) | ||
type Schema = z.output<typeof schema> | ||
const state = ref({ | ||
email: '[email protected]', | ||
password: 'apibeer', | ||
}) | ||
const form = ref<Form<Schema>>() | ||
const submiting = ref(false) | ||
async function onSubmit(event: FormSubmitEvent<Schema>) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* eslint-disable no-console */ | ||
import process from 'node:process' | ||
import bcrypt from 'bcryptjs' | ||
import { hash } from '~/server/utils/password' | ||
import { prisma } from '~/server/utils/prisma' | ||
|
||
async function seed() { | ||
|
@@ -20,7 +20,7 @@ async function seedUsers() { | |
email: '[email protected]', | ||
username: 'apibeer', | ||
password: { | ||
create: { hash: await bcrypt.hash('123456', 10) }, | ||
create: { hash: await hash('123456') }, | ||
}, | ||
}, | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import bcrypt from 'bcryptjs' | ||
|
||
/** | ||
* Hashes a raw password using bcrypt. | ||
* @param raw The raw password to hash. | ||
* @returns A Promise that resolves to the hashed password. | ||
*/ | ||
export async function hash(raw: string) { | ||
return await bcrypt.hash(raw, 10) | ||
} | ||
|
||
/** | ||
* Verifies a raw password against a hashed password using bcrypt. | ||
* @param raw The raw password to verify. | ||
* @param hash The hashed password to compare against. | ||
* @returns A Promise that resolves to a boolean indicating whether the passwords match. | ||
*/ | ||
export async function verify(raw: string, hash: string) { | ||
return await bcrypt.compare(raw, hash) | ||
} |