You can accomplish this using useRouter
inside _app.js
(recommended)
import {useRouter} from 'next/router';
function MyApp({ Component, pageProps }) {
const router = useRouter();
const {asPath,route,pathname } = router // pick the one that you need
if(asPath === "Authenticate"){
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
)
}else{
return(
<Provider store={store}>
<Authenticate>
<Layout>
<Component {...pageProps} />
</Layout>
</Authenticate>
</Provider>
)
}
}
Otherwise you can apply the same logic directly inside <Authenticate>
component, and if the current route is "Authenticate" just return the childrens.
Another solution could be to use getInitialProps
, but in this way CSR will be disabled and you will rely on the request instead of next.js router:
function MyApp({ Component, pageProps }) {
const {path } = pageProps
if(path === "Authenticate"){
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
)
}else{
return(
<Provider store={store}>
<Authenticate>
<Layout>
<Component {...pageProps} />
</Layout>
</Authenticate>
</Provider>
)
}
}
MyApp.getInitialProps = async (appContext) => {
return {pageProps : {
path : appContext.ctx.pathname
}}
}