I'm using next-iron-session for session storage and firebase for auth, every api route should have auth middleware and also next-iron-session, it my first time using NextJS. tried a few different ways but none of them works, here is what I did so fare:
//auth-middleware.ts
import { NextApiRequest, NextApiResponse } from "next";
import { auth } from "utils/firebase-admin";
import { sessionObject } from "utils/session";
import { withIronSession } from "next-iron-session";
const withAuth = (handler: any) => {
return async (req: NextApiRequest, res: NextApiResponse) => {
const { accessToken } = (<any>req).session.get("user");
await auth.verifyIdToken(accessToken);
return handler(req, res);
};
};
export default withIronSession(withAuth, sessionObject);
//hello.ts (just a simple route)
import { NextApiRequest, NextApiResponse } from "next";
import authMiddleware from "utils/auth-middleware";
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
res.status(200).json({ name: "John Doe" });
};
export default authMiddleware(handler);
I am not sure is it the correct way or not.
any help please?