Initial commit

This commit is contained in:
mxdyeah committed 2026-07-08 01:15:18 +08:00
commit acadc92e68
780 files changed
+468285

No files matched your search

+27
View File
@@ -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 };
});
+6
View File
@@ -0,0 +1,6 @@
import { clearAuthSession } from '../../utils/auth';
export default defineEventHandler((event) => {
clearAuthSession(event);
return { ok: true };
});
+15
View File
@@ -0,0 +1,15 @@
import { getSessionUser } from '../../utils/auth';
export default defineEventHandler((event) => {
const user = getSessionUser(event);
if (!user) {
return { loggedIn: false };
}
return {
loggedIn: true,
username: user.username,
display_name: user.username,
musicList: user.musicList
};
});
+35
View File
@@ -0,0 +1,35 @@
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.length < 3 || username.length > 32) {
throw createError({ statusCode: 400, statusMessage: 'Username must be 3-32 characters.' });
}
if (password.length < 6 || password.length > 72) {
throw createError({ statusCode: 400, statusMessage: 'Password must be 6-72 characters.' });
}
const existing = db
.prepare('SELECT id FROM users WHERE username = ?')
.get(username) as { id: number } | undefined;
if (existing) {
throw createError({ statusCode: 409, statusMessage: 'Username already exists.' });
}
const passwordHash = bcrypt.hashSync(password, 10);
const result = db
.prepare('INSERT INTO users (username, password_hash, created_at) VALUES (?, ?, ?)')
.run(username, passwordHash, Date.now());
cleanupExpiredSessions();
createSession(event, Number(result.lastInsertRowid));
return { ok: true, username, display_name: username };
});