fix: made withAuthWrapper working

fix: - made wrapper working - used it in protected routes & removed it from public routes
This commit is contained in:
Dakshesh Jain 2022-11-25 16:30:41 +05:30
parent 362e90800d
commit a58abecc0e
5 changed files with 24 additions and 23 deletions

View File

@ -1,30 +1,32 @@
import React, { useEffect } from "react"; import React from "react";
// next // next
import type { NextPage } from "next"; import type { NextPage } from "next";
// axios configurations
import { setAxiosHeader } from "configuration/axios-configuration";
// redirect // redirect
import redirect from "lib/redirect"; import redirect from "lib/redirect";
const withAuthWrapper = (WrappedComponent: NextPage) => { const withAuth = (WrappedComponent: NextPage) => {
const Wrapper: NextPage<any> = (props) => { const Wrapper: NextPage<any> = (props) => {
useEffect(() => {
if (props?.tokenDetails && props?.tokenDetails?.access_token) {
setAxiosHeader(props.tokenDetails.access_token);
}
}, [props]);
return <WrappedComponent {...props} />; return <WrappedComponent {...props} />;
}; };
Wrapper.getInitialProps = async (ctx) => { Wrapper.getInitialProps = async (ctx) => {
const componentProps = const isServer = typeof window === "undefined";
WrappedComponent.getInitialProps &&
(await WrappedComponent.getInitialProps(ctx)); const cookies = isServer ? ctx?.req?.headers.cookie : document.cookie;
return { ...componentProps };
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; return Wrapper;
}; };
export default withAuthWrapper; export default withAuth;

View File

@ -1,7 +1,8 @@
// next imports // next imports
import type { NextPageContext } from "next";
import Router from "next/router"; import Router from "next/router";
const redirect = (context: any, target: any) => { const redirect = (context: NextPageContext, target: any) => {
if (context.res) { if (context.res) {
// server // server
// 303: "See other" // 303: "See other"

View File

@ -8,8 +8,6 @@ import Image from "next/image";
import useUser from "lib/hooks/useUser"; import useUser from "lib/hooks/useUser";
// services // services
import authenticationService from "lib/services/authentication.service"; import authenticationService from "lib/services/authentication.service";
// hoc
import withAuthWrapper from "lib/hoc/withAuthWrapper";
// layouts // layouts
import DefaultLayout from "layouts/DefaultLayout"; import DefaultLayout from "layouts/DefaultLayout";
// social button // social button
@ -180,4 +178,4 @@ const SignIn: NextPage = () => {
); );
}; };
export default withAuthWrapper(SignIn); export default SignIn;

View File

@ -11,8 +11,6 @@ import workspaceService from "lib/services/workspace.service";
import { WORKSPACE_INVITATION } from "constants/fetch-keys"; import { WORKSPACE_INVITATION } from "constants/fetch-keys";
// hooks // hooks
import useUser from "lib/hooks/useUser"; import useUser from "lib/hooks/useUser";
// hoc
import withAuthWrapper from "lib/hoc/withAuthWrapper";
// layouts // layouts
import DefaultLayout from "layouts/DefaultLayout"; import DefaultLayout from "layouts/DefaultLayout";
// ui // ui
@ -149,4 +147,4 @@ const WorkspaceInvitation: NextPage = () => {
); );
}; };
export default withAuthWrapper(WorkspaceInvitation); export default WorkspaceInvitation;

View File

@ -9,6 +9,8 @@ import AdminLayout from "layouts/AdminLayout";
import useSWR from "swr"; import useSWR from "swr";
// hooks // hooks
import useUser from "lib/hooks/useUser"; import useUser from "lib/hooks/useUser";
// hoc
import withAuthWrapper from "lib/hoc/withAuthWrapper";
// fetch keys // fetch keys
import { USER_ISSUE } from "constants/fetch-keys"; import { USER_ISSUE } from "constants/fetch-keys";
// services // services
@ -167,4 +169,4 @@ const Workspace: NextPage = () => {
); );
}; };
export default Workspace; export default withAuthWrapper(Workspace);