user6863891
user6863891

Reputation:

How to handle cookie appropriately in React

I have a react web app with login and signup functionality.

import React, { Component } from "react";
import { Button, FormGroup, FormControl, ControlLabel } from "react-bootstrap";
import "./Login.css";

export default class Login extends Component {
  constructor(props) {
    super(props);

    this.state = {
      email: "",
      password: ""
    };
  }

  validateForm() {
    return this.state.email.length > 0 && this.state.password.length > 0;
  }

  handleChange = event => {
    this.setState({
      [event.target.id]: event.target.value
    });
  }

  handleSubmit = event => {
    event.preventDefault();
  }

  render() {
    return (
      <div className="Login">
        <form onSubmit={this.handleSubmit}>
          <FormGroup controlId="email" bsSize="large">
            <ControlLabel>Email</ControlLabel>
            <FormControl
              autoFocus
              type="email"
              value={this.state.email}
              onChange={this.handleChange}
            />
          </FormGroup>
          <FormGroup controlId="password" bsSize="large">
            <ControlLabel>Password</ControlLabel>
            <FormControl
              value={this.state.password}
              onChange={this.handleChange}
              type="password"
            />
          </FormGroup>
          <Button
            block
            bsSize="large"
            disabled={!this.validateForm()}
            type="submit"
          >
            Login
          </Button>
        </form>
      </div>
    );
  }
}

I logged the response coming from the backend and has cookie which postman sets and sends automatically, How do I set the cookie in from the browser in react, is there a way to send cookie by default with each request if the cookie exist?

Upvotes: 0

Views: 72

Answers (1)

Quentin
Quentin

Reputation: 944533

Cookies are handled transparently by the browser. You don't need to do anything.

(Unless you are making cross-origin Ajax requests, but there's no suggestion that you are in the question, and the specifics would depend on the Ajax API you were using if you were).

Upvotes: 1

Related Questions