Strugling with strange issue in next js production version. Please help me guys I am stuck.
Unanswered
Gazami crab posted this in #help-forum
Gazami crabOP
I have a logout function which clears local storage and delete cookies and redirect to the root "/" page.
the cognitoActions function.
so locally this works perfectly and it does clear local storage and cookies and redirect me to the / page. but in production when I click on logout it open a modal asking yes or no than if I select yes it clears the local storage and cookies and everything but the page didn't get redirect and when I again press on yes button than it gets redirect.
now if I try to login again than the brower gets stuck in loading and the browser and tabs get frezze. and if I try to open the url to new tab than its take me to the dashboard page.
locally I didnt met with such issues, its everything happening on production. please help me guys.
const onLogOut = React.useCallback(async () => {
await cognitoActions.logout();
}, []);the cognitoActions function.
import { Auth } from "aws-amplify";
import Router from "next/router";
import { persistor } from "redux/store";
import { deleteCookie } from "cookies-next";
const cognitoActions = {
logout: async () => {
await Auth.signOut();
deleteCookie("user");
localStorage.clear();
Router.push("/"); // Redirect to home page
},
};
export default cognitoActions;so locally this works perfectly and it does clear local storage and cookies and redirect me to the / page. but in production when I click on logout it open a modal asking yes or no than if I select yes it clears the local storage and cookies and everything but the page didn't get redirect and when I again press on yes button than it gets redirect.
now if I try to login again than the brower gets stuck in loading and the browser and tabs get frezze. and if I try to open the url to new tab than its take me to the dashboard page.
locally I didnt met with such issues, its everything happening on production. please help me guys.
6 Replies
Does this work better?
const router = useRouter()
router.push("/")- app router:
- pages router:
import { useRouter } from 'next/navigation' - pages router:
import { useRouter } from 'next/router'@KINXZ Does this work better?
ts
const router = useRouter()
router.push("/")
Gazami crabOP
sir I am using next 12.2 version so I think its pages router only.
Gazami crabOP
@KINXZ sir I tried this locally and its working same as before. Now will build deploy to the server and check does it work or not.
const onLogOut = () => {
deleteCookie("user");
localStorage.clear();
setLogoutModel(false);
console.log("everything gets clear");
router.push("/"); // Redirect to home page
};Gazami crabOP
I found the issue, but why this is causing the issue I dont know.
This code I have written in middleware, when I comment out this code the app is loging out perfectly fine but I don’t know why this code is cause issue. Is there any different method of deleting cookies on production
if (PUBLIC_URLS) {
if (authToken) {
return redirectToRoleHome(roles, request);
}
}This code I have written in middleware, when I comment out this code the app is loging out perfectly fine but I don’t know why this code is cause issue. Is there any different method of deleting cookies on production
Gazami crabOP
The issue get resolved it was middleware function which were blocking the router to redirect to the root page