This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/packages/dashboard/src/services/authServerSideProps.js

28 lines
813 B
JavaScript

import ky from "ky/umd";
const isProduction = process.env.NODE_ENV === "production";
export default function authServerSideProps(getServerSideProps) {
return function authenticate(context) {
if (isProduction && (!("ory_kratos_session" in context.req.cookies) || !("skynet-jwt" in context.req.cookies))) {
return {
redirect: {
permanent: false,
destination: `/api/accounts/login?return_to=${encodeURIComponent(context.resolvedUrl)}`,
},
};
}
if (getServerSideProps) {
const api = ky.create({
headers: { cookie: context.req.headers.cookie },
prefixUrl: isProduction ? "http://oathkeeper:4455" : "http://localhost:3000/api/stubs",
});
return getServerSideProps(context, api);
}
return { props: {} };
};
}