Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a cookie in react?

Orginally, I use the following ajax to set cookie.

function setCookieAjax(){   $.ajax({     url: `${Web_Servlet}/setCookie`,     contentType: 'application/x-www-form-urlencoded;charset=utf-8',     headers: { 'Access-Control-Allow-Origin': '*',                'username': getCookie("username"),               'session': getCookie("session")     },     type: 'GET',     success: function(response){       setCookie("username", response.name, 30);       setCookie("session", response.session, 30);}   }) }  function setCookie(cname, cvalue, minutes) {     var d = new Date();     d.setTime(d.getTime() + (minutes*60*1000));     var expires = "expires="+ d.toUTCString();     document.cookie = cname + "=" + cvalue + "; " + expires; }  export const getUserName = (component) => {     setCookieAjax()  $.ajax({     url: `${Web_Servlet}/displayHeaderUserName`,     contentType: 'application/x-www-form-urlencoded;charset=utf-8',     headers: { 'Access-Control-Allow-Origin': '*',                 'username': getCookie("username"),                 'session': getCookie("session")             },     type: 'GET',     success: function(response){         component.setState({         usernameDisplay: response.message         })    }.bind(component)  }) } 

Now, I want to set the cookie using the servlet's adding cookie function. But I don't know how to achieve my purpose.

Cookie loginCookie = new Cookie("user",user);  //setting cookie to expiry in 30 mins         loginCookie.setMaxAge(30*60);         loginCookie.setDomain("localhost:4480");         loginCookie.setPath("/");  response.addCookie(loginCookie); 

In order to extend the cookie timelimit, what should I write in the react side to receive the cookie's session time?

like image 882
OPfan Avatar asked Oct 03 '16 08:10

OPfan


People also ask

How do you set cookies?

Cookies are usually set by a web-server using the response Set-Cookie HTTP-header. Then, the browser automatically adds them to (almost) every request to the same domain using the Cookie HTTP-header.

How do I use http only cookies in react?

This is how I send the cookie after the user login. res . cookie("token", token, { httpOnly: true, secure: true, sameSite: "none", }) . send();

Is react-cookie safe?

Is react-cookie safe to use? The npm package react-cookie was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as safe to use.


2 Answers

It appears that the functionality previously present in the react-cookie npm package has been moved to universal-cookie. The relevant example from the universal-cookie repository now is:

import Cookies from 'universal-cookie'; const cookies = new Cookies(); cookies.set('myCat', 'Pacman', { path: '/' }); console.log(cookies.get('myCat')); // Pacman 
like image 162
speckledcarp Avatar answered Sep 22 '22 22:09

speckledcarp


Use vanilla js, example

document.cookie = `referral_key=hello;max-age=604800;domain=example.com` 

Read more at: https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

like image 38
Smakosh Avatar answered Sep 23 '22 22:09

Smakosh