Debug the login system and it works :D (without the token store logic tho

This commit is contained in:
吳元皓 2025-05-27 11:38:03 +08:00
parent 7057f8293d
commit eaa925e5dd
2 changed files with 31 additions and 20 deletions

View File

@ -13,6 +13,7 @@ export default defineEventHandler(async (event) => {
} }
const body = await readBody(event); const body = await readBody(event);
const { username, password } = body; const { username, password } = body;
console.log(password);
if (!username || !password) { if (!username || !password) {
return { return {
error: "NO_USER_AND_PASSWORD_SUBMITED", error: "NO_USER_AND_PASSWORD_SUBMITED",
@ -25,45 +26,55 @@ export default defineEventHandler(async (event) => {
}; };
} }
// Server side hashing // Server side hashing
const hashedPassword = await argon2.hash(salt, password);
// Check if user exists, if not, create a user // Check if user exists, if not, create a user
try { try {
console.log(username);
const fetchUserInfo = await sql` const fetchUserInfo = await sql`
select * from users select * from users
where user = ${username}`; where username = ${username}`;
if (!fetchUserInfo) { console.log(fetchUserInfo[0]);
const createNewUser = await sql` if (fetchUserInfo.length === 0) {
const hashedPassword = await argon2.hash(salt + password);
const createNewUser = await sql`
insert into users (uuid, username, passwordhash) insert into users (uuid, username, passwordhash)
values (${uuidv4()}, ${username}, ${hashedPassword}) values (${uuidv4()}, ${username}, ${hashedPassword})
`; `;
if (!createNewUser) { console.log(createNewUser);
if (fetchUserInfo.length !== 0) {
return { return {
error: "CANNOT_CREATE_NEW_USER", error: "CANNOT_CREATE_NEW_USER",
}; };
} }
const newToken = uuidv4();
//const newToken64 = atob(newToken);
return {
user: fetchUserInfo,
token: newToken,
};
} else { } else {
if (fetchUserInfo.password !== hashedPassword) { const isValid = await argon2.verify(fetchUserInfo[0].passwordhash, salt + password);
if (!isValid) {
return { return {
error: "PASSWORD_NO_MATCH", error: "PASSWORD_NO_MATCH",
}; };
} }
const newToken = uuidv4(); }
const newToken64 = atob(newToken); const newToken = uuidv4();
const saveNewToken = await sql` const newToken64 = btoa(newToken);
insert into usertokens const fetchUserInfoAgain = await sql`
`; select * from users
if (!saveNewToken) { where username = ${username}`;
return { /*await sql`
error: "CANNOT_CREATE_NEW_TOKEN", INSERT INTO usertokens (user, token)
}; VALUES (${fetchUserInfo[0].username}, ${newToken64})
} `;*/
return { return {
user: fetchUserInfo.user, user: fetchUserInfoAgain,
token: newToken, token: newToken,
}; };
}
} catch (e) { } catch (e) {
console.log(e);
return { return {
error: "UNABLE_TO_PROCESS", error: "UNABLE_TO_PROCESS",
}; };

View File

@ -25,8 +25,8 @@ async function lineToday(slug: string) {
.text() .text()
.replaceAll("\n", "") .replaceAll("\n", "")
.replace(" ", ""); .replace(" ", "");
const paragraph = []; const paragraph = <any[]>[];
const images = []; const images = <any[]>[];
html("article.news-content") html("article.news-content")
.contents() .contents()
.each((i, element) => { .each((i, element) => {