32 lines
812 B
TypeScript
32 lines
812 B
TypeScript
import { json } from '@sveltejs/kit';
|
|
import { addUser, getUsers } from '$lib/server/userService';
|
|
import bcrypt from 'bcrypt';
|
|
|
|
const saltRounds = 12;
|
|
|
|
export function GET() {
|
|
|
|
const userList = getUsers();
|
|
|
|
return new Response(JSON.stringify(userList));
|
|
}
|
|
|
|
export async function POST({ request }) {
|
|
const data = await request.json();
|
|
const userName = data.userName;
|
|
const userPassword = data.userPassword;
|
|
|
|
if (!userName || !userPassword) {
|
|
return json({ error: 'Missing input' }, { status: 400 });
|
|
}
|
|
|
|
const hashedPassword = bcrypt.hashSync(userPassword, saltRounds);
|
|
const rowInfo = addUser(userName, hashedPassword);
|
|
|
|
if (rowInfo?.changes == 1) {
|
|
return json({ userId: rowInfo.lastInsertRowid, userName: userName }, { status: 201 });
|
|
} else {
|
|
return new Response(null, { status: 400 });
|
|
}
|
|
}
|