You've already forked obsidian-visualiser
Finished registration + working on own useAuth composable
This commit is contained in:
35
server/api/auth/register.post.ts
Normal file
35
server/api/auth/register.post.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import useDatabase from '~/composables/useDatabase';
|
||||
import { schema } from '~/schemas/registration';
|
||||
|
||||
export default defineEventHandler(async (e) => {
|
||||
const body = await readValidatedBody(e, schema.safeParse);
|
||||
|
||||
if(!body.success)
|
||||
return body.error;
|
||||
|
||||
const db = useDatabase();
|
||||
|
||||
const usernameQuery = db.query(`SELECT COUNT(*) as count FROM users WHERE username = ?1`);
|
||||
const checkUsername = usernameQuery.get(body.data.username);
|
||||
|
||||
const emailQuery = db.query(`SELECT COUNT(*) as count FROM users WHERE email = ?1`);
|
||||
const checkEmail = emailQuery.get(body.data.email);
|
||||
|
||||
const errors = [];
|
||||
if(checkUsername.count !== 0)
|
||||
errors.push({ path: ['username'], message: "Ce nom d'utilisateur est déjà utilisé" });
|
||||
if(checkEmail.count !== 0)
|
||||
errors.push({ path: ['email'], message: "Cette adresse mail est déjà utilisée" });
|
||||
|
||||
if(errors.length > 0)
|
||||
throw createError({ status: 406, message: "duplicates", data: errors });
|
||||
else
|
||||
{
|
||||
const hash = await Bun.password.hash(body.data.password);
|
||||
const registration = db.query(`INSERT INTO users(username, email, hash) VALUES(?1, ?2, ?3)`);
|
||||
const result = registration.get(body.data.username, body.data.email, hash);
|
||||
|
||||
setResponseStatus(e, 201, "Created");
|
||||
return { success: true };
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user