1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/** @jsx jsx */
/** @jsxFrag Fragment */
import { Hono } from "npm:hono@3";
import { jsx, Fragment } from 'https://deno.land/x/hono/middleware.ts';
import RootLayout from "https://esm.town/v/iamseeley/RootLayout";
import * as bcrypt from "https://deno.land/x/bcrypt/mod.ts";
import { sqlite } from "https://esm.town/v/std/sqlite";
import { jwt } from 'npm:hono/jwt';
import { sign } from 'npm:hono/jwt';
const SubmitLogin = async (c) => {
try {
const body = await c.req.parseBody();
const username = body["username"];
const password = body["password"];
const checkUser = await sqlite.execute({
sql: `SELECT * FROM users WHERE username = ?`,
args: [username],
});
if (checkUser.rows.length === 0) {
console.log("No user found with the provided username.");
return c.html(
<RootLayout>
<p>Error during login. Username not found.</p>
</RootLayout>
);
}
const storedHashedPassword = checkUser.rows[0][3]; // Access the fourth element (index 3) of the row array
const isValid = await bcrypt.compare(password, storedHashedPassword);
console.log(`Password validation result: ${isValid}`);
if (isValid) {
const payload = { userId: checkUser.rows[0][0], username };
const SECRET_KEY = Deno.env.get("JWT_SECRET_TOKEN");
const token = await sign(payload, SECRET_KEY);
c.cookie('token', token, {
httpOnly: true,
maxAge: 3600,
path: '/',
});
}
// Redirect to edit profile page upon successful login
return c.redirect(`/edit-profile/${username}`);
} catch (error) {
console.error("Login error:", error);
return c.html(
<RootLayout>
<p>Error during login. Please try again or contact support if the problem persists.</p>
</RootLayout>
);
}
};
export default SubmitLogin;