1

I am working on a React project In my project I have a scenario to store object in Cookie.

This is my code Login.js

import React, { useState } from 'react';
import './Login.css';

const Login = () => {
    const [data, setData] = useState('')

    const handleChange = ({ target }) => {
        const { name, value } = target
        const newData = Object.assign({}, data, { [name]: value })
        setData(newData)
    }

    const handleSubmit = (e) => {
        e.preventDefault()
        console.log(data)
    }
    return (
        <div className='container'>
            <div className='row justify-content-center'>
                <div className='col-4'>
                    <div className='main'>
                        <form onSubmit={handleSubmit}>
                            <div className="form-group">
                                <label htmlFor="email">Email address</label>
                                <input type="email" className="form-control" name='email' id="email" onChange={handleChange}></input>
                            </div>
                            <div className="form-group">
                                <label htmlFor="username">Username</label>
                                <input type="text" className="form-control" name='username' id="username" onChange={handleChange}></input>
                            </div>
                            <div className="form-group">
                                <label htmlFor="password">Password</label>
                                <input type="password" className="form-control" name='password' id="password" onChange={handleChange}></input>
                            </div>
                            <button type="submit" className="btn btn-primary">Submit</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    )
}

export default Login

When I click submit button data will print in console. I have to store that in cookie

So what is the best way to store data object in Cookie

2 Answers 2

2

You can save the cookie in document.cookie. But you need to save the data as a string. You could parse the whole object with JSON.parse, or the more elegant way is to use the specific attributes.

Cookies

But be aware that it is not save to store passwords in cookies.

Try this:

import React, { useState } from 'react';
import './Login.css';

const Login = () => {
    const [data, setData] = useState('')

    const handleChange = ({ target }) => {
        const { name, value } = target
        const newData = Object.assign({}, data, { [name]: value })
        setData(newData)
    }

    const handleSubmit = (e) => {
        e.preventDefault()
        document.cookie = `email=${data.email}`; 
        document.cookie = `username=${data.username}`; 
        document.cookie = `password=${data.password}`;  
    }

    return (
        <div className='container'>
            <div className='row justify-content-center'>
                <div className='col-4'>
                    <div className='main'>
                        <form onSubmit={handleSubmit}>
                            <div className="form-group">
                                <label htmlFor="email">Email address</label>
                                <input type="email" className="form-control" name='email' id="email" onChange={handleChange}></input>
                            </div>
                            <div className="form-group">
                                <label htmlFor="username">Username</label>
                                <input type="text" className="form-control" name='username' id="username" onChange={handleChange}></input>
                            </div>
                            <div className="form-group">
                                <label htmlFor="password">Password</label>
                                <input type="password" className="form-control" name='password' id="password" onChange={handleChange}></input>
                            </div>
                            <button type="submit" className="btn btn-primary">Submit</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    )
}

export default Login
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @MaatMa why it is storing only email ? it is not storing username and password
But like I already wrote, please take a look at JWT (JSON Web Token). It's a much saver way.
0

You just set data to cookie:

document.cookie =

of you can use some npm lib like this:

https://www.npmjs.com/package/react-cookie

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.