aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLarsVomMars2020-07-30 20:14:56 +0200
committerLarsVomMars2020-07-30 20:14:56 +0200
commit86c68c3648c94ca9f66f3eb408973368bed28681 (patch)
tree6d1def94caea53c3de4eb741eb113d7812b000a7
parent14aea28ac22e5d2e9e510c5986daca1bef1c43ae (diff)
Added user handlers
-rw-r--r--src/groups/user.ts7
-rw-r--r--src/handler/user.ts96
-rw-r--r--src/util/user.ts30
3 files changed, 113 insertions, 20 deletions
diff --git a/src/groups/user.ts b/src/groups/user.ts
index b8518dc..e0c7359 100644
--- a/src/groups/user.ts
+++ b/src/groups/user.ts
@@ -1,8 +1,11 @@
-import type { Group, Context } from "https://deno.land/x/abc@master/mod.ts";
+import type { Group } from "https://deno.land/x/abc@master/mod.ts";
import * as handlers from "../handler/user.ts";
export default function (g: Group) {
- g.get("/:name", handlers.index);
+ g.get("/login", handlers.renderLogin);
g.post("/register", handlers.register);
g.post("/login", handlers.login);
+ g.any("/logout", handlers.logout);
+ g.put("/theme", handlers.changeTheme);
+ g.put("/password", handlers.updatePassword);
}
diff --git a/src/handler/user.ts b/src/handler/user.ts
index e194008..2c3ac8d 100644
--- a/src/handler/user.ts
+++ b/src/handler/user.ts
@@ -1,25 +1,85 @@
import type { HandlerFunc, Context } from "https://deno.land/x/abc@master/mod.ts";
-import db, {loginData} from "../db/user.ts";
+import db, { loginData } from "../db/user.ts";
+import * as log from "https://deno.land/std/log/mod.ts";
+import { getCurrentUser, isAdmin } from "../util/user.ts";
+import { isSetup } from "../util/server.ts";
+import { deleteCookie } from "https://deno.land/std/http/cookie.ts";
+
-export const index: HandlerFunc = async (c: Context) => c.params.name;
export const register: HandlerFunc = async (c: Context) => {
- const { username, email, password } = await c.body();
- const success = await db.createUser(email, username, password);
- // TODO: Send email
- return {success};
-};
+ if (!(await isAdmin(c)) && await isSetup()) return { success: false }; // I'm tired: not sure if this works
+ // TODO: How to handle register
+ const { username, email, password, admin } = await c.body();
+ try {
+ const success = await db.createUser(email, username, password, admin !== undefined ? admin : false);
+ return { success };
+ } catch (e) {
+ return { success: false };
+ }
+}
+export const renderLogin: HandlerFunc = async (c: Context) => {
+ if (await getCurrentUser(c)) return c.redirect("/");
+ return await c.render("./src/views/login.ejs");
+}
export const login: HandlerFunc = async (c: Context) => {
const { username, password } = await c.body();
- const data: loginData = await db.login(username, password);
- if (data.success) {
- c.setCookie({
- name: "uid",
- value: data.uid!.toString(),
- });
- c.setCookie({
- name: "verification",
- value: data.verification!,
- })
+ try {
+ const data: loginData = await db.login(username, password);
+ if (data.success) {
+ c.setCookie({
+ name: "uid",
+ value: data.uid!.toString(),
+ path: "/",
+ });
+ c.setCookie({
+ name: "verification",
+ value: data.verification!,
+ path: "/",
+ });
+ }
+ return { success: data.success };
+ } catch (e) {
+ log.error(e);
+ return { success: false };
+ }
+}
+export const logout: HandlerFunc = async (c: Context) => {
+ deleteCookie(c.response, "uid");
+ deleteCookie(c.response, "verification");
+ c.redirect("/");
+}
+export const changeTheme: HandlerFunc = async (c: Context) => {
+ try {
+ const currentUser = await getCurrentUser(c);
+ if (!currentUser) return { success: false };
+ await db.changeTheme(currentUser.id);
+ return { success: true };
+ } catch (e) {
+ log.error(e);
+ return { success: false };
+ }
+}
+export const setAdmin: HandlerFunc = async (c: Context) => {
+ const { uid, state } = await c.body();
+ try {
+ const currentUser = await getCurrentUser(c);
+ if (!(currentUser && currentUser.isAdmin)) return { success: false };
+ await db.setAdminState(uid, state);
+ return { success: true };
+ } catch (e) {
+ log.error(e);
+ return { success: false };
+ }
+}
+export const updatePassword: HandlerFunc = async (c: Context) => {
+ const currentUser = await getCurrentUser(c);
+ if (!currentUser) return { success: false };
+ const { currentPassword, newPassword } = await c.body();
+ try {
+ await db.changePassword(currentUser.id, currentPassword, newPassword);
+ return { success: true };
+ } catch (e) {
+ log.error(e);
+ return { success: false };
}
- return {"success": data.success};
}
diff --git a/src/util/user.ts b/src/util/user.ts
new file mode 100644
index 0000000..f632a11
--- /dev/null
+++ b/src/util/user.ts
@@ -0,0 +1,30 @@
+import type { userData } from "../db/user.ts";
+import db from "../db/user.ts";
+import * as log from "https://deno.land/std/log/mod.ts";
+import type { Context } from "https://deno.land/x/abc@master/mod.ts";
+
+export const getCurrentUser = async (c: Context): Promise<userData | undefined> => {
+ const cookies = getUserCookies(c);
+ try {
+ return await db.getUserByVerificationId(cookies.uid, cookies.verification) as userData;
+ } catch (e) {
+ log.error(e);
+ return undefined;
+ }
+}
+
+export const getUserCookies = (c: Context): userCookies => {
+ const uid = parseInt(c.cookies["uid"]);
+ const verification = c.cookies["verification"];
+ return { uid, verification };
+}
+
+export const isAdmin = async (c: Context): Promise<boolean> => {
+ const user = await getCurrentUser(c);
+ return (user && user.isAdmin) as boolean;
+}
+
+export interface userCookies {
+ uid: number;
+ verification: string;
+}