link-stack/apps/leafcutter/middleware.ts
2023-05-25 12:37:14 +00:00

39 lines
871 B
TypeScript

import { withAuth } from "next-auth/middleware";
export default withAuth(
() => { },
{
pages: {
signIn: `/login`,
},
callbacks: {
authorized: ({ token, req }) => {
const {
url,
headers,
} = req;
const embedded = process.env.LINK_EMBEDDED === "true";
if (embedded) {
return true;
}
// check login page
const parsedURL = new URL(url);
if (parsedURL.pathname.startsWith('/login')) {
return true;
}
// check session auth
const authorizedDomains = ["redaranj.com", "digiresilience.org"];
const userDomain = token?.email?.toLowerCase().split("@").pop() ?? "unauthorized.net";
if (authorizedDomains.includes(userDomain)) {
return true;
}
return false;
},
}
}
);