fix: made withAuthWrapper working
fix: - made wrapper working - used it in protected routes & removed it from public routes
This commit is contained in:
parent
362e90800d
commit
a58abecc0e
5 changed files with 24 additions and 23 deletions
|
|
@ -1,30 +1,32 @@
|
|||
import React, { useEffect } from "react";
|
||||
import React from "react";
|
||||
// next
|
||||
import type { NextPage } from "next";
|
||||
// axios configurations
|
||||
import { setAxiosHeader } from "configuration/axios-configuration";
|
||||
// redirect
|
||||
import redirect from "lib/redirect";
|
||||
|
||||
const withAuthWrapper = (WrappedComponent: NextPage) => {
|
||||
const withAuth = (WrappedComponent: NextPage) => {
|
||||
const Wrapper: NextPage<any> = (props) => {
|
||||
useEffect(() => {
|
||||
if (props?.tokenDetails && props?.tokenDetails?.access_token) {
|
||||
setAxiosHeader(props.tokenDetails.access_token);
|
||||
}
|
||||
}, [props]);
|
||||
|
||||
return <WrappedComponent {...props} />;
|
||||
};
|
||||
|
||||
Wrapper.getInitialProps = async (ctx) => {
|
||||
const componentProps =
|
||||
WrappedComponent.getInitialProps &&
|
||||
(await WrappedComponent.getInitialProps(ctx));
|
||||
return { ...componentProps };
|
||||
const isServer = typeof window === "undefined";
|
||||
|
||||
const cookies = isServer ? ctx?.req?.headers.cookie : document.cookie;
|
||||
|
||||
const token = cookies?.split("accessToken=")?.[1]?.split(";")?.[0];
|
||||
|
||||
if (!token) {
|
||||
redirect(ctx, "/signin");
|
||||
}
|
||||
|
||||
const pageProps =
|
||||
WrappedComponent.getInitialProps && (await WrappedComponent.getInitialProps(ctx));
|
||||
|
||||
return { ...pageProps };
|
||||
};
|
||||
|
||||
return Wrapper;
|
||||
};
|
||||
|
||||
export default withAuthWrapper;
|
||||
export default withAuth;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
// next imports
|
||||
import type { NextPageContext } from "next";
|
||||
import Router from "next/router";
|
||||
|
||||
const redirect = (context: any, target: any) => {
|
||||
const redirect = (context: NextPageContext, target: any) => {
|
||||
if (context.res) {
|
||||
// server
|
||||
// 303: "See other"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue