44 lines
No EOL
1.4 KiB
JavaScript
44 lines
No EOL
1.4 KiB
JavaScript
// from an old project: https://git.zenoverse.net/bigly-archive/auth-thing/raw/branch/main/src/routes/register/+page.server.js
|
|
import Route from "../route.js";
|
|
import initDb from "../db.js";
|
|
import { hash } from "bcrypt";
|
|
|
|
const minChar = 1;
|
|
const maxChar = 32;
|
|
|
|
let db = await initDb();
|
|
|
|
function isValid(user) {
|
|
return user.search(/[^A-Za-z0-9\-\_]/g) == -1;
|
|
}
|
|
|
|
// TODO: rewrite
|
|
let main = new Route([], async function (req, res, input) {
|
|
let { user, pass, pass2 } = req.body;
|
|
|
|
if (pass != pass2) return { 'success': false, 'message': 'Passwords do not match' };
|
|
|
|
if (!pass || !user || !pass2) return { 'success': false, 'message': 'Some fields are missing' };
|
|
|
|
if (!isValid(user)) return { 'success': false, 'message': 'Username is invalid' };
|
|
|
|
if (user.length < minChar || user.length > maxChar)
|
|
return { 'success': false, 'message': `Username must be ${minChar} to ${maxChar} characters` };
|
|
|
|
let isExist = await db.all('SELECT username FROM auth WHERE UPPER(username) LIKE UPPER(?)', [
|
|
user
|
|
]);
|
|
|
|
if (isExist.length > 0) return { 'success': false, 'message': 'Account already exists' };
|
|
|
|
var passHash = await hash(pass, 10);
|
|
|
|
await db.run('INSERT INTO auth (username, password) VALUES (?, ?)', [
|
|
user,
|
|
passHash
|
|
]);
|
|
|
|
return { 'message': 'Account created', 'success': true, 'redirect': '/client/login' };
|
|
});
|
|
|
|
export default main; |