Skip to content

Commit

Permalink
fix: add ironSession middleware to auth router
Browse files Browse the repository at this point in the history
  • Loading branch information
aj-may committed Jan 4, 2023
1 parent cae66c8 commit e0d8a6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export const signIn: RequestHandler<SignInResponse> = async (req, res) => {
if (!nonce) return res.status(400).send("Bad Request");

const parsedBody = signInRequestSchema.safeParse(req.body);
if (!parsedBody.success) return res.status(400).send(fromZodError(parsedBody.error).message);
if (!parsedBody.success) {
const error = fromZodError(parsedBody.error);
return res.status(400).send(error.message);
}
const { message, signature } = parsedBody.data;

const { success, error, data } = await new SiweMessage(message).verify({
Expand All @@ -48,7 +51,7 @@ export const signIn: RequestHandler<SignInResponse> = async (req, res) => {
// domain, // TODO: verify domain is correct too
});

if (!success && error) return res.status(500).send(error.type); // TODO: Better status code
if (!success && error) return res.status(400).send(error.type);
if (!success) return res.status(500).send("Unknown Error");

req.session.nonce = undefined;
Expand Down
32 changes: 19 additions & 13 deletions src/express/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import express from "express";
import { IronSessionOptions } from "iron-session";
import { ironSession } from "iron-session/express";
import { getSession, methodNotAllowed, notFound, signIn, signOut } from "../api.js";

const router = express.Router();
export const authRouter = (ironOptions: IronSessionOptions) => {
const router = express.Router();

router.route('/')
.get(getSession)
.all(methodNotAllowed);
router.use(ironSession(ironOptions));

router.route('/signin')
.post(signIn)
.all(methodNotAllowed);
router.route('/')
.get(getSession)
.all(methodNotAllowed);

router.route('/signout')
.post(signOut)
.all(methodNotAllowed);
router.route('/signin')
.post(signIn)
.all(methodNotAllowed);

router.route('*')
.all(notFound);
router.route('/signout')
.post(signOut)
.all(methodNotAllowed);

export default router;
router.route('*')
.all(notFound);

return router;
};

0 comments on commit e0d8a6c

Please sign in to comment.