Add: Proper Account creation

This commit is contained in:
Donatas Kirda 2024-05-16 12:25:41 +03:00
parent 5d2c9c5f9a
commit 62edbd9cd2
Signed by: bloodwiing
GPG Key ID: 63020D8D3F4A164F
2 changed files with 47 additions and 13 deletions

View File

@ -85,20 +85,52 @@ export async function getUser(user_id) {
* @param {string} password
* @returns {Promise<import('$types/status').Success | import('$types/status').Error>}
*/
export async function registerUser(sql, username, password) {
// const users = await getUsers(sql, [user_id]);
export async function registerUser(username, password) {
const hashedPassword = await hash(password, {
type: argon2id,
memoryCost: 2 ** 16,
timeCost: 4,
parallelism: 1,
hashLength: 64,
});
// return users.get(user_id) || {
// error: true,
// msg: `Could not find user of ID ${user_id}`
// };
const salt = await genSalt(10);
const insert = sql`
INSERT INTO doki8902.user (username, password)
VALUES (${ username }, ${ hashedPassword });`;
console.log(salt);
try {
await insert;
// const hash =
} catch (e) {
if (e && typeof(e) === 'object' && 'name' in e && e.name === 'PostgresError') {
const pgerr = /** @type {PostgresError} */ (e);
// const insert = sql`
// INSERT INTO doki8902.user (username, password)
// VALUES (${ username }, ${ hash })`
switch (pgerr.constraint_name) {
case 'idx_user_username':
return {
error: true,
msg: "Username taken",
};
case 'username_length_min':
return {
error: true,
msg: "Username has invalid length",
};
case 'username_valid_symbols':
return {
error: true,
msg: "Username contains invalid symbols",
};
default:
break;
}
}
}
return {
success: true,
};
}

View File

@ -19,7 +19,9 @@ async function POST({ request }) {
return;
}
registerUser(username, password);
const result = await registerUser(username, password);
console.log(result);
}
/** @type {import('./$types').Actions} */