0

I have a request for authorization to the server:

axios
    .post(
      "http://localhost:5000/api/auth/sign-in",
      { ...values },
    )
    .then((res) => {
      const { token } = res.data;
     //there I need to save coookie with token that came from server
    })

after successful authorization, the server sends the token, how can I save this token in the cookie with the httpOnly flag?

rejs
  • 41
  • 2
  • 10
  • You can use [react-cookie](https://www.npmjs.com/package/react-cookie) package. It's very simple to work with. – Nima Ebrazeh Sep 12 '21 at 15:05
  • Does this answer your question? [How can I set a cookie in react?](https://stackoverflow.com/questions/39826992/how-can-i-set-a-cookie-in-react) – Tushar Gupta Sep 12 '21 at 15:06

1 Answers1

0

You can use universal-cookie package to do this: https://www.npmjs.com/package/universal-cookie

import Cookies from 'universal-cookie';
 
const cookies = new Cookies();

...

.then((res) => {
  const { token } = res.data;
 cookies.set('token', token , { httpOnly: true, path: "/" });
})
Viet
  • 12,133
  • 2
  • 15
  • 21