React: Delete cookie when user closes the browser

react-cookie
javascript delete cookie not working
javascript delete session cookie
delete cookie on logout javascript
cookies.remove not working
javascript delete httponly cookie
clear cookies script
jquery delete all cookies

I'm using the library react-cookie (https://www.npmjs.com/package/react-cookie) and I want to delete the user cookies when the browser or tab is closed. I used ComponentWillUnmount in my Approuter, but that doesn't work when the browser is closed. Anyone know how to achieve this?

import React from 'react';
import {Router, Route, Switch} from 'react-router-dom';
import history from '../data/history';
import { withCookies, Cookies } from 'react-cookie';
import { instanceOf } from 'prop-types';


class AppRouter extends React.Component{

static propTypes = {
    cookies: instanceOf(Cookies).isRequired
};


constructor(props){
    super(props)
}

componentWillMount(){
    const {cookies} = this.props;
    cookies.remove('userInfo');
}

render(){
    return(
        <Router history={history}>
            <div>
                <Header/>
                <div className="main-container">
                    <Switch>
                        //routes
                    </Switch>
                    <Footer/>
                </div> 
            </div>
        </Router>
    );
}
}

export default withCookies(AppRouter);

The router does receive the cookies so why can't I remove it with componentWillUnmount? And how do I remove them?


Not really React specific but you can use this :

window.addEventListener("beforeunload", (ev) => 
{  
    ev.preventDefault();
    cookies.remove('userInfo');
});

clearing cookies on closing browser tab, after login when user closes the website by closing the browser tab and not by logging out. the session is maintained and i m trying to log in again  Wouldn't the cookie be deleted if you remove the expiration date? AFAIK a cookie without expiration date lasts only for that particular session. – Mirakurun Nov 26 '18 at 11:59


When you create cookie. please set expires to 0. e.g

cookies.set('userInfo', name, { expires: 0 });

Then this cookie will be expired when browser closed.

cookies.remove(), Syntax. var removing = browser.cookies.remove( details // object ). Parameters. details: An object containing  Answer. Cookies are removed after the browser is closed, however some cookies still remain. The DOM storage is a kind of "super cookie" web developers can use to retain information. Like regular cookies, they are not kept after the window is closed.


This may help you...

componentDidMount(){
    window.addEventListener(
        "beforeunload",
        cookies.remove('userInfo')
    );
}

Logout user and delete cookies from the client side?, You should be able to combine the solutions in these answers to achieve what you are looking for. Keep checking and if the internet connection drops, clear the​  If you do not set the expiry date, the cookie will be removed when the user closes the browser. document.cookie = "cookiename=cookievalue; expires= Thu, 21 Aug 2014 20:00:00 UTC". You can also set the domain and path to specify to which domain and to which directories in the specific domain the cookie belongs to.


Sessions Cookie is not expired after browser close · Issue #481 · js , Sessions Cookie is not expired after browser close #481 ://stackoverflow.com/​questions/10617954/chrome-doesnt-delete-session-cookies. Maybe like a clear all cookies (logout) for this domain or path. More Info: For example, say they are at school or at the library on a public computer and the connection drops (which happens a lot).


Destroy Cookies When users close the browser tab or Browser , var validNavigation = false; // Events as soon as the DOM tree is ready How to add destroy cookies In the given below code? User sessions were a method introduced to maintain state across http calls which are naturally stateless back when server rendering was the rage (ok still is in many cases). When you deal with a SPA, you need to think of the user state as part of the state of the application because you are not going across http calls as you move through you app.


clear session or cookie on browser close, The session and cookie automatically get cleared when a user logout. But there is possibility that user close the browser without loging out . If the browser is forcibly closed or crashes, session cookies are not deleted and the session remains. It’s worth noting, this does not happen on sites backed by https. While this is default behavior, unlike Chrome, closing the browser will clear any session cookies present.