From eb499d5cf803c0749cb627ce1ab879f7927776a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondr=CC=8Cej=20Ny=CC=81vlt?= <7478957+vanniewelt@users.noreply.github.com> Date: Thu, 22 Dec 2022 21:09:40 +0100 Subject: [PATCH] Refactor --- src/api/helpers.ts | 2 ++ src/auth/auth.api.ts | 27 +-------------------------- src/auth/auth.handlers.ts | 13 +++++++------ src/index.ts | 4 ++-- src/members/members.handlers.ts | 8 +++----- src/types.d.ts | 9 +++++++++ src/user/user.api.ts | 26 ++++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 39 deletions(-) create mode 100644 src/types.d.ts create mode 100644 src/user/user.api.ts diff --git a/src/api/helpers.ts b/src/api/helpers.ts index fe922b8..dfd151f 100644 --- a/src/api/helpers.ts +++ b/src/api/helpers.ts @@ -1,6 +1,8 @@ import { isLeft } from "fp-ts/Either" import * as D from "io-ts/Decoder" +export const API_URL = "https://discord.com/api/v10" + export class HttpError extends Error { status: number | null = null constructor(status: number) { diff --git a/src/auth/auth.api.ts b/src/auth/auth.api.ts index 3817a31..fbd7e4a 100644 --- a/src/auth/auth.api.ts +++ b/src/auth/auth.api.ts @@ -1,8 +1,7 @@ import * as D from "io-ts/Decoder" import { APP_URL, CLIENT_ID, CLIENT_SECRET } from "../env" -import { expectJson } from "../api/helpers" +import { API_URL, expectJson } from "../api/helpers" -const API_URL = "https://discord.com/api/v10" const REDIRECT_URL = `${APP_URL}/authorize` export const AUTH_URL = `${API_URL}/oauth2/authorize?${new URLSearchParams({ @@ -35,27 +34,3 @@ export const fetchToken = (code: string): Promise => { }), }).then(expectJson(TokenResponse)) } - -const User = D.struct({ - id: D.string, - username: D.string, - avatar: D.string, - discriminator: D.string, -}) - -export type User = D.TypeOf - -const VerifyResponse = D.struct({ - user: User, -}) - -type VerifyResponse = D.TypeOf - -export const verifyToken = (token: string): Promise => { - return fetch(`${API_URL}/oauth2/@me`, { - method: "GET", - headers: { - Authorization: `Bearer ${token}`, - }, - }).then(expectJson(VerifyResponse)) -} diff --git a/src/auth/auth.handlers.ts b/src/auth/auth.handlers.ts index 10a29ad..0e88238 100644 --- a/src/auth/auth.handlers.ts +++ b/src/auth/auth.handlers.ts @@ -1,13 +1,14 @@ -import { AUTH_URL, fetchToken, verifyToken } from "./auth.api" -import { HttpError } from "../api/helpers" -import type { RequestHandler } from "express" import path from "path" +import { AUTH_URL, fetchToken } from "./auth.api" +import { HttpError } from "../api/helpers" +import { fetchMe } from "../user/user.api" +import type { RequestHandler } from "express" // ----------------------------------------------------------------------------- // Middleware // ----------------------------------------------------------------------------- -export const verifyUser: RequestHandler = async (req, res, next) => { +export const requireAuth: RequestHandler = async (req, res, next) => { const token = req.cookies.token if (!token) { @@ -16,8 +17,8 @@ export const verifyUser: RequestHandler = async (req, res, next) => { } try { - const { user } = await verifyToken(token) - ;(req as any).user = user + const { user } = await fetchMe(token) + req.user = user next() } catch (error) { if (error instanceof HttpError && error.status === 401) { diff --git a/src/index.ts b/src/index.ts index 52c6e09..1f258fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import cookieParser from "cookie-parser" import express from "express" import layouts from "express-ejs-layouts" -import { authorize, getLogin, logout, verifyUser } from "./auth/auth.handlers" +import { authorize, getLogin, logout, requireAuth } from "./auth/auth.handlers" import { getMembers } from "./members/members.handlers" import { handleError, handleNotFound } from "./errors/errors.handlers" @@ -37,7 +37,7 @@ app.get("/authorize", authorize) app.post("/logout", logout) // Members -app.get("/members", verifyUser, getMembers) +app.get("/members", requireAuth, getMembers) app.get("/test-error", () => { throw new Error("Error 1") diff --git a/src/members/members.handlers.ts b/src/members/members.handlers.ts index 52ed1d3..25e78a8 100644 --- a/src/members/members.handlers.ts +++ b/src/members/members.handlers.ts @@ -4,7 +4,7 @@ import { Client, Events, GatewayIntentBits } from "discord.js" import type { RequestHandler } from "express" import type { Role } from "discord.js" -import type { User } from "../auth/auth.api" +import type { User } from "../user/user.api" import path from "path" // ----------------------------------------------------------------------------- @@ -54,14 +54,12 @@ export const getRolesAndMembers = () => export const getMembers: RequestHandler = async (req, res) => { const roles = await getRolesAndMembers() - const user: User = (req as any).user - res.render(path.join(__dirname, "members.view.ejs"), { layout: path.join(process.cwd(), "src/views/_layout"), roles: processRoles(roles), user: { - ...user, - avatarUrl: getAvatarUrl(user), + ...req.user, + avatarUrl: req.user ? getAvatarUrl(req.user) : null, }, }) } diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..b32947a --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,9 @@ +import type { User } from "./user/user.api" + +declare global { + namespace Express { + interface Request { + user?: User + } + } +} diff --git a/src/user/user.api.ts b/src/user/user.api.ts new file mode 100644 index 0000000..dda052e --- /dev/null +++ b/src/user/user.api.ts @@ -0,0 +1,26 @@ +import * as D from "io-ts/Decoder" +import { API_URL, expectJson } from "../api/helpers" + +const User = D.struct({ + id: D.string, + username: D.string, + avatar: D.string, + discriminator: D.string, +}) + +export type User = D.TypeOf + +const VerifyResponse = D.struct({ + user: User, +}) + +type VerifyResponse = D.TypeOf + +export const fetchMe = (token: string): Promise => { + return fetch(`${API_URL}/oauth2/@me`, { + method: "GET", + headers: { + Authorization: `Bearer ${token}`, + }, + }).then(expectJson(VerifyResponse)) +}