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
61
62
63
64
65
66
67
68
/** @jsx jsx */
/** @jsxFrag Fragment */
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 * as jose from 'https://deno.land/x/jose@v5.2.4/index.ts'
import { SignJWT } from 'https://deno.land/x/jose@v5.2.4/jwt/sign.ts';
const SubmitSignup = async (c) => {
const SECRET_KEY = Deno.env.get("JWT_SECRET_TOKEN");
try {
const body = await c.req.parseBody();
const username = body["username"];
const email = body["email"];
const password = body["password"];
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
const result = await sqlite.execute({
sql: `INSERT INTO users (username, email, password) VALUES (?, ?, ?)`,
args: [username, email, hashedPassword],
});
const userId = result.lastInsertRowId;
const payload = {
userId,
username,
exp: Math.floor(Date.now() / 1000) + (60 * 60),
};
const secretKey = new TextEncoder().encode(SECRET_KEY);
const jwt = await new SignJWT(payload)
.setProtectedHeader({ alg: "HS256" })
.setIssuedAt()
.setExpirationTime(`${payload.exp}s`)
.sign(secretKey);
c.cookie("token", jwt, {
httpOnly: true,
maxAge: 3600,
path: "/",
});
return c.redirect(`/edit-profile/${username}`);
} catch (error) {
console.error("Signup error:", error);
if (error.code === "SQLITE_CONSTRAINT" && error.message.includes("users.email")) {
return c.html(
<RootLayout>
<p>The email address is already registered. Please use a different email.</p>
</RootLayout>
);
}
return c.html(
<RootLayout>
<p>Error during signup. Please try again or contact support if the problem persists.</p>
</RootLayout>
);
}
};
export default SubmitSignup;