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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/** @jsxImportSource https://esm.sh/hono@latest/jsx **/
import { Hono } from "npm:hono@3";
import { sqlite } from "https://esm.town/v/std/sqlite";
import { email } from "https://esm.town/v/std/email?v=11";
import { jwt } from 'npm:hono/jwt';
import RootLayout from "https://esm.town/v/iamseeley/RootLayout";
import LandingPage from "https://esm.town/v/iamseeley/LandingPage";
import EditProfilePage from "https://esm.town/v/iamseeley/EditProfilePage";
import ProfilePage from "https://esm.town/v/iamseeley/ProfilePage";
import SubmitSignup from "https://esm.town/v/iamseeley/SubmitSignup";
import SubmitLogin from "https://esm.town/v/iamseeley/SubmitLogin";
import { logger } from 'npm:hono/logger';
import SignupModal from "https://esm.town/v/iamseeley/SignupModal";
import LoginModal from "https://esm.town/v/iamseeley/LoginModal";
import AddLink from "https://esm.town/v/iamseeley/AddLink";
import EditLinkModal from "https://esm.town/v/iamseeley/EditLinkModal";
import { getLinkById, getUserByUsername, isEmailTaken, isUsernameTaken } from "https://esm.town/v/iamseeley/Queries";
import { getUserProfileHandler, updateUserProfileHandler } from "https://esm.town/v/iamseeley/profileHandlers";
import { addLinkHandler, updateLinkHandler, deleteLinkHandler } from "https://esm.town/v/iamseeley/linkHandlers";
const app = new Hono();
app.use(logger());
const SECRET_KEY = Deno.env.get("JWT_SECRET_TOKEN");
if (!SECRET_KEY) {
console.error("JWT_SECRET_TOKEN is not set");
}
const jwtMiddleware = jwt({ secret: SECRET_KEY, cookie: 'token' });
app.get('/', (c) => c.html(<LandingPage title="link tree alt" description="simple link tree" />));
// user routes
app.post('/signup', SubmitSignup);
app.post('/login', SubmitLogin);
// app.delete('/users/:userId', jwtMiddleware, deleteUserHandler);
// link routes
app.get('/links/:linkId', jwtMiddleware, async (c) => {
const linkId = parseInt(c.req.param('linkId'), 10);
const link = await getLinkById(linkId);
return c.json(link);
});
app.put('/links/:linkId', jwtMiddleware, updateLinkHandler);
app.post('/links', jwtMiddleware, addLinkHandler);
app.delete('/links/:linkId', jwtMiddleware, deleteLinkHandler);
// user profile routes
app.get('/edit-profile/:username', jwtMiddleware, getUserProfileHandler);
app.post('/edit-profile/:username', jwtMiddleware, updateUserProfileHandler);
// app.get('/:username', userProfilePageHandler);
// components (htmx)
app.get('/signupModal', async (c) => c.html(<SignupModal />));
app.get('/loginModal', async (c) => c.html(<LoginModal />));
app.get('/addLinkModal/:username', async (c) => {
const username = c.req.param('username');
return c.html(<AddLink username={username} />);
});
app.get('/edit-link-modal/:username/:linkId', async (c) => {
const username = c.req.param('username');
const linkId = parseInt(c.req.param('linkId'), 10);
const link = await getLinkById(linkId);
return c.html(
<EditLinkModal
username={username}
linkId={link.id}
label={link.label}
url={link.url}
/>);
});
app.get('/check-email', async (c) => {
const { email } = c.req.query();
const taken = await isEmailTaken(email);
if (taken) {
return c.html('<p class="text-red-500">Sorry, this email is taken.</p>');
}
return c.html('');
});
app.get('/check-username', async (c) => {
const { username } = c.req.query();
const taken = await isUsernameTaken(username);
if (taken) {
return c.html('<p class="text-red-500">Sorry, this username is taken.</p>');
}
return c.html('');
});
export default app.fetch;