Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React-cookie, removeCookie doesn't work when url have dynamic url or more than two depth url with url access, not by react -router-dom #346

Closed
songjihyeok opened this issue Jan 12, 2023 · 1 comment

Comments

@songjihyeok
Copy link

songjihyeok commented Jan 12, 2023

   "react": "^18.2.0"
   "react-router-dom": "5.1.2"
   "react-cookie": "^4.1.1"

When we use dynamic url or more than two depth url, the removeCookie doesn't work. In the condition that if we access to the page by the URL. Not by the react-router-dom.

For example, we often try to access to the url like "URL/report/:reportId", "URL/report/reportDetail". Also we want to access to the page in url. Because we want to share the specific report. When we access to the url with authorized token, we can access to the page.

However, when we try to logout, The removeCookie doesn't work. here is the code.

Router.js

 <BrowserRouter>
            <Switch> 
                 {.......other routers.......}
                <AuthorizedRouter exact path="/report/reportDetail/:reportId" component={<ReportDetail/>} /> // This router check the token is valid
                <AuthorizedRouter exact path="/reportList" component={<ReportList/>} />
            </Switch>
</BrowserRouter>

ReportDetail.js

import {useCookie} from "react-cookie"

 const ReportDetail = ()=> { 
const [,,removeCookie] = useCookie();

const onLogout = ()=>{
    removeCookie("loginToken")
}

return <div> 
   <button onClick={onLogout}/>
</div>
}

Now, the removeCookie doesn't work at all.
However in the same logic with different url like "/reportList" which has just one depth Url. The removeCookie works well even though we accessed to the page in url.

I don't know why this bug happen. So i just used the vanilla way to remove the cookie

const onLogout = ()=>{ document.cookie = "loginToken=; expires=0; path=/;"; }
It works, but i don't like to use vanilla way in react. Is there any proper solution with this problem?

@eXon
Copy link
Collaborator

eXon commented Aug 20, 2023

You need to make sure to set the path to / when calling removeCookie. Here, you are including it in the vanilla javascript version, but not in the React version.

@eXon eXon closed this as completed Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants