'production and local routing mismatching
I'm having hard time understanding why my local routing and production routing are mismatching.
I'm writing an app in next.js
In my _app.tsx file I have following lines
if (!isUserLogged && Component.isRestricted) {
redirect(res, '/login');
return { pageProps: {} };
}
if (isUserLogged && !hasUserGivenConsent && !Component.tosPage) {
redirect(res, '/accept-tos');
return { pageProps: {} };
}
if (isUserLogged && hasUserGivenConsent && Component.tosPage) {
redirect(res, '/');
return { pageProps: {} };
}
if (isUserLogged && Component.isAuthPage) {
redirect(res, '/');
return { pageProps: {} };
}
Component.someProperty are always statics in pages/somePage.tsx files.
isUserLoggedIn and hasUserGivenConsent are redux toolkit state.
My pages/login.tsx:
class LoginPage extends Component {
static isRestricted = false;
static isAuthPage = true;
static getInitialProps = wrapper.getInitialPageProps(store => async ({
res,
query: {
token,
uid,
},
}) => {
const { dispatch }: { dispatch: AppThunkDispatch } = store;
if (
!!token && typeof token === 'string' &&
!!uid && typeof uid === 'string'
) {
await dispatch(activateUser({
token,
uid,
}))
.unwrap()
.catch(e => {
redirect(res, '/expired-link ');
});
} else {
dispatch(clearActivationStatus());
}
return {};
});
render() {
return (
<LoginForm />
);
}
}
Locally everything works as expected:
- not logged user, trying to access restricted page ->
/login - logged user, tos not accepted ->
/accept-tos - logged user, tos accepted, trying to access tos page ->
/ - user clicking on activation mail is directed to
/login?uid=...&token=...-> getInitialProps check if there are uid and token if so, then it posts a request to backend to activate account -> if ok user stays on login page -> if not redirects user to/expired-link
Production behavior:
- (current problem) no matter if user is logged in or not it redirects to
/accept-tosfrom any URL I type into browser. - (previous problem, not achievable due to current problem) If user clicked on the activation link
/login?uid=...&token=...it redirected to/which is restricted path
Solution 1:[1]
Nevermind, I ran a gitlab job that cleared the environment so thus it wasn't working.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | adammo |
