2022-11-25 11:00:41 +00:00
|
|
|
import React from "react";
|
2022-11-19 14:21:26 +00:00
|
|
|
// next
|
|
|
|
import type { NextPage } from "next";
|
|
|
|
// redirect
|
|
|
|
import redirect from "lib/redirect";
|
|
|
|
|
2022-12-07 14:35:12 +00:00
|
|
|
const withAuth = (WrappedComponent: NextPage, getBackToSameRoute: boolean = true) => {
|
2022-11-19 14:21:26 +00:00
|
|
|
const Wrapper: NextPage<any> = (props) => {
|
|
|
|
return <WrappedComponent {...props} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
Wrapper.getInitialProps = async (ctx) => {
|
2022-11-25 11:00:41 +00:00
|
|
|
const isServer = typeof window === "undefined";
|
|
|
|
|
|
|
|
const cookies = isServer ? ctx?.req?.headers.cookie : document.cookie;
|
|
|
|
|
|
|
|
const token = cookies?.split("accessToken=")?.[1]?.split(";")?.[0];
|
|
|
|
|
|
|
|
if (!token) {
|
2022-12-07 14:35:12 +00:00
|
|
|
if (getBackToSameRoute) redirect(ctx, "/signin?next=" + ctx?.asPath);
|
|
|
|
else redirect(ctx, "/signin");
|
2022-11-25 11:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pageProps =
|
|
|
|
WrappedComponent.getInitialProps && (await WrappedComponent.getInitialProps(ctx));
|
|
|
|
|
|
|
|
return { ...pageProps };
|
2022-11-19 14:21:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Wrapper;
|
|
|
|
};
|
|
|
|
|
2022-11-25 11:00:41 +00:00
|
|
|
export default withAuth;
|