diff options
author | Akshay <[email protected]> | 2024-11-08 22:27:41 +0000 |
---|---|---|
committer | Akshay" (aider) <[email protected]> | 2024-11-08 22:27:41 +0000 |
commit | 25397b7cf0e48665b4499b881909f505de9e899b (patch) | |
tree | a01523b29611c57efbd21b01deed812dd02e8360 /src | |
parent | 115b44441d1d28925a52668d6ca305be93dfb5ae (diff) |
feat: Add user registration, login, and subscription endpoints
Diffstat (limited to 'src')
-rw-r--r-- | src/routes/index.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/routes/index.js b/src/routes/index.js index 77ddd78..f703d2e 100644 --- a/src/routes/index.js +++ b/src/routes/index.js | |||
@@ -67,6 +67,53 @@ router.get("/media/*", async (req, res) => { | |||
67 | res.render("media", { kind, url }); | 67 | res.render("media", { kind, url }); |
68 | }); | 68 | }); |
69 | 69 | ||
70 | // POST /register | ||
71 | router.post("/register", async (req, res) => { | ||
72 | const { username, password } = req.body; | ||
73 | try { | ||
74 | db.run("INSERT INTO users (username, password) VALUES (?, ?)", [ | ||
75 | username, | ||
76 | password, | ||
77 | ]); | ||
78 | res.status(201).send("User registered successfully"); | ||
79 | } catch (err) { | ||
80 | res.status(400).send("Error registering user"); | ||
81 | } | ||
82 | }); | ||
83 | |||
84 | // POST /login | ||
85 | router.post("/login", async (req, res) => { | ||
86 | const { username, password } = req.body; | ||
87 | const user = db | ||
88 | .query("SELECT * FROM users WHERE username = ? AND password = ?", [ | ||
89 | username, | ||
90 | password, | ||
91 | ]) | ||
92 | .get(); | ||
93 | if (user) { | ||
94 | res.status(200).send("Login successful"); | ||
95 | } else { | ||
96 | res.status(401).send("Invalid credentials"); | ||
97 | } | ||
98 | }); | ||
99 | |||
100 | // POST /subscribe | ||
101 | router.post("/subscribe", async (req, res) => { | ||
102 | const { username, subreddit } = req.body; | ||
103 | const user = db | ||
104 | .query("SELECT * FROM users WHERE username = ?", [username]) | ||
105 | .get(); | ||
106 | if (user) { | ||
107 | db.run("INSERT INTO subscriptions (user_id, subreddit) VALUES (?, ?)", [ | ||
108 | user.id, | ||
109 | subreddit, | ||
110 | ]); | ||
111 | res.status(201).send("Subscribed successfully"); | ||
112 | } else { | ||
113 | res.status(404).send("User not found"); | ||
114 | } | ||
115 | }); | ||
116 | |||
70 | module.exports = router; | 117 | module.exports = router; |
71 | 118 | ||
72 | function unescape_submission(response) { | 119 | function unescape_submission(response) { |