Initial commit
This commit is contained in:
commit
acadc92e68
780 files changed
+468285
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import bcrypt from 'bcryptjs';
|
||||
import { createError, readBody } from 'h3';
|
||||
import db from '../../db';
|
||||
import { cleanupExpiredSessions, createSession } from '../../utils/auth';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event);
|
||||
const username = String(body?.username ?? '').trim();
|
||||
const password = String(body?.password ?? '');
|
||||
|
||||
if (!username || !password) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Username and password are required.' });
|
||||
}
|
||||
|
||||
const user = db
|
||||
.prepare('SELECT id, username, password_hash FROM users WHERE username = ?')
|
||||
.get(username) as { id: number; username: string; password_hash: string } | undefined;
|
||||
|
||||
if (!user || !bcrypt.compareSync(password, user.password_hash)) {
|
||||
throw createError({ statusCode: 401, statusMessage: 'Invalid credentials.' });
|
||||
}
|
||||
|
||||
cleanupExpiredSessions();
|
||||
createSession(event, user.id);
|
||||
|
||||
return { ok: true, username: user.username, display_name: user.username };
|
||||
});
|
||||
Reference in new issue
Block a user