Add: User Logging in

This commit is contained in:
2024-05-16 13:28:24 +03:00
parent a34ed38800
commit eea87823d9
9 changed files with 212 additions and 49 deletions
+1 -1
View File
@@ -131,7 +131,7 @@ export async function getPost(post_id, opts = {}) {
}
}();
const category_guess = await getCategoryCached(sql, post['category_id']);
const category_guess = await getCategoryCached(post['category_id']);
if (Object.hasOwn(category_guess, 'error')) {
return {
error: true,
+44
View File
@@ -134,3 +134,47 @@ export async function createUser(username, password) {
success: true,
};
}
/**
* @param {string} username
* @param {string} password
* @returns {Promise<import('$types/status').Success | import('$types/status').Error>}
*/
export async function createUserSession(username, password) {
const select = sql`
SELECT password, id
FROM doki8902.user
WHERE username = ${ username };`;
const result = await select;
if (result.length == 0) {
return {
error: true,
msg: 'Username or password is incorrect',
};
}
const hashedPassword = result[0]['password'];
const isMatch = await verify(hashedPassword, password);
if (!isMatch) {
return {
error: true,
msg: 'Username or password is incorrect',
};
}
const insert = sql`
INSERT INTO doki8902.user_session (user_id)
VALUES (${ result[0]['id'] })
RETURNING token;`;
const token = await insert;
return {
success: true,
result: token[0]['token'],
};
}