Skip to content

Commit

Permalink
fix: login/logout
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftpsh committed Jul 16, 2024
1 parent 21981b2 commit 2fa76ec
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/api/auth/credentials/$get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { RequestHandler } from "express";
import { toUserResponse } from "src/model/user/types";

const handler: RequestHandler = (req, res) => {
if (!req.user) {
res.status(401).send("Unauthorized");
return;
}

res.send(toUserResponse(req.user));
};

export default handler;
8 changes: 8 additions & 0 deletions src/api/auth/credentials/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from "express";
import $get from "./$get";

const router = Router();

router.get("/", $get);

export default router;
4 changes: 4 additions & 0 deletions src/api/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Router } from "express";
import login from "./login";
import logout from "./logout";
import credentials from "./credentials";

const router = Router();

router.use("/credentials", credentials);
router.use("/login", login);
router.use("/logout", logout);

export default router;
5 changes: 4 additions & 1 deletion src/api/auth/login/$post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ const login: RequestHandler = async (req, res) => {
}

const jwt = UserToJWT(user, new Date(Date.now() + 1000 * 60 * 60 * 24 * 7)); // 7 days
res.header("Authorization", `Bearer ${jwt}`).sendStatus(200);
res.cookie("Authorization", jwt, {
signed: true,
});
res.sendStatus(200);
};

export default login;
8 changes: 8 additions & 0 deletions src/api/auth/logout/$post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RequestHandler } from "express";

const logout: RequestHandler = (req, res) => {
res.clearCookie("Authorization");
res.sendStatus(200);
};

export default logout;
8 changes: 8 additions & 0 deletions src/api/auth/logout/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from "express";
import $post from "./$post";

const router = Router();

router.post("/", $post);

export default router;
8 changes: 8 additions & 0 deletions src/middlewares/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { RequestHandler } from "express";
import { Record, String } from "runtypes";
import { JWTToUser } from "../utils/jwt";

const Cookie = Record({
Authorization: String,
});

const Auth: RequestHandler = async (req, res, next) => {
let token: string | null = null;
if (req.headers.authorization) {
Expand All @@ -9,6 +14,9 @@ const Auth: RequestHandler = async (req, res, next) => {
token = auth.substring(7);
}
}
if (Cookie.guard(req.signedCookies)) {
token = req.signedCookies.Authorization;
}

if (token !== null) {
try {
Expand Down
15 changes: 15 additions & 0 deletions src/model/user/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { User } from "@prisma/client";

export const toUserResponse = ({
userId,
loginId,
displayName,
isHost,
}: User) => {
return {
userId,
loginId,
displayName,
isHost,
};
};

0 comments on commit 2fa76ec

Please sign in to comment.