Add: Proper Account creation
This commit is contained in:
parent
5d2c9c5f9a
commit
62edbd9cd2
@ -85,20 +85,52 @@ export async function getUser(user_id) {
|
|||||||
* @param {string} password
|
* @param {string} password
|
||||||
* @returns {Promise<import('$types/status').Success | import('$types/status').Error>}
|
* @returns {Promise<import('$types/status').Success | import('$types/status').Error>}
|
||||||
*/
|
*/
|
||||||
export async function registerUser(sql, username, password) {
|
export async function registerUser(username, password) {
|
||||||
// const users = await getUsers(sql, [user_id]);
|
const hashedPassword = await hash(password, {
|
||||||
|
type: argon2id,
|
||||||
|
memoryCost: 2 ** 16,
|
||||||
|
timeCost: 4,
|
||||||
|
parallelism: 1,
|
||||||
|
hashLength: 64,
|
||||||
|
});
|
||||||
|
|
||||||
// return users.get(user_id) || {
|
const insert = sql`
|
||||||
// error: true,
|
INSERT INTO doki8902.user (username, password)
|
||||||
// msg: `Could not find user of ID ${user_id}`
|
VALUES (${ username }, ${ hashedPassword });`;
|
||||||
// };
|
|
||||||
const salt = await genSalt(10);
|
|
||||||
|
|
||||||
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`
|
switch (pgerr.constraint_name) {
|
||||||
// INSERT INTO doki8902.user (username, password)
|
case 'idx_user_username':
|
||||||
// VALUES (${ username }, ${ hash })`
|
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,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,9 @@ async function POST({ request }) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
registerUser(username, password);
|
const result = await registerUser(username, password);
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {import('./$types').Actions} */
|
/** @type {import('./$types').Actions} */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user