Add: User Logging in
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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'],
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user