0
import React, { Component } from "react";
import fire from "../config/fire";
import { Form, Button } from "react-bootstrap";
import Container from "react-bootstrap/Container";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
import logo from "./logo.png";

export default class CreateUser extends React.Component {
  constructor(props) {
    super(props);
    this.login = this.login.bind(this);
    this.handleChange = this.handleChange.bind(this);
    this.state = {
      email: "",
      pwd: "",
      name: "",
      phoneNo: "",
      address: "",
    };
  }
  login(e) {
    const db = fire.firestore();
    e.preventDefault();
    fire
      .auth()
      .createUserWithEmailAndPassword(this.state.email, this.state.pwd)
      //   .signInWithEmailAndPassword(this.state.email, this.state.pwd)
      .then((u) => {
        // console.log(u.user.uid);
        return db.collection("createdUsers").doc(u.user.uid).set({
          email: this.state.email,
          name: this.state.name,
          phoneNo: this.state.phoneNo,
          address: this.state.address,
        });
      })
      .then(() => {
        this.setState({
          email: "",
          pwd: "",
          name: "",
          phoneNo: "",
          address: "",
        });
      })
      .then(() => {
        alert("Successfully Created the user!");
      })
      .catch((error) => {
        alert(error);
      });
  }
  handleChange(e) {
    this.setState({
      [e.target.name]: e.target.value,
    });
  }
  render() {
    return (
      <Container style={{ margin: 0, padding: 0, maxWidth: 1366 }}>
        <Row>
          {/* <Col
            style={{
              backgroundColor: "#EFEFEF",
              paddingTop: 250,
              height: 700,
            }}
          > */}
          {/* <img src={logo} alt="Logo" width="150px" /> */}
          {/* </Col> */}
          <Col
          // style={{
          //   backgroundColor: "#ffffff",
          //   paddingTop: 250,
          //   height: 700,
          // }}
          >
            <div>
              <Form style={{ width: 300, height: 300, margin: "0 auto" }}>
                <Form.Group controlId="formBasicEmail">
                  <Form.Label>Username</Form.Label>
                  <Form.Control
                    type="email"
                    placeholder="Enter your email"
                    id="email"
                    name="email"
                    placeholder="Enter your email address"
                    onChange={this.handleChange}
                    value={this.state.email}
                    required
                  />
                </Form.Group>

                <Form.Group controlId="formBasicPassword">
                  <Form.Label>Password</Form.Label>
                  <Form.Control
                    type="password"
                    placeholder="Password"
                    id="pwd"
                    name="pwd"
                    placeholder="Password"
                    onChange={this.handleChange}
                    value={this.state.pwd}
                    required
                  />
                </Form.Group>
                <Form.Group controlId="formBasicInput1">
                  <Form.Label>Name</Form.Label>
                  <Form.Control
                    type="text"
                    placeholder="Name"
                    id="name"
                    name="name"
                    placeholder="Name"
                    onChange={this.handleChange}
                    value={this.state.name}
                    required
                  />
                </Form.Group>
                <Form.Group controlId="formBasicInput2">
                  <Form.Label>Phone Number</Form.Label>
                  <Form.Control
                    type="text"
                    placeholder="Phone Number"
                    id="phoneNo"
                    name="phoneNo"
                    onChange={this.handleChange}
                    value={this.state.phoneNo}
                    required
                  />
                </Form.Group>
                <Form.Group controlId="formBasicInput3">
                  <Form.Label>Address</Form.Label>
                  <Form.Control
                    type="text"
                    placeholder="Address"
                    id="address"
                    name="address"
                    onChange={this.handleChange}
                    value={this.state.address}
                    required
                  />
                </Form.Group>

                <Button variant="primary" type="submit" onClick={this.login}>
                  Create User
                </Button>
              </Form>
            </div>
          </Col>
        </Row>
      </Container>
    );
  }
}

bst365体育投注网址大全In the above code, I'm creating a user and with some custom fields. And I'm already logging into the app from some other component(using signInWithEmailAndPassword) and then inside app, I'm creating users, But, after clicking "create user" button, it's making me log out and if I again log in with the credentials from which I used to log in always, after logging in its showing me the alert message, and then it's creating user and storing the values in firestore collection. Now in this, It should create user and store the custom values in firestore collection. And then it should make fields empty and then it should show alert message. But it should not make me log out. Please help me out with this...

  • 1
    You mentioned that it makes you logout, what do you mean by that ? – Dipansh Khandelwal Jul 1 at 5:40
  • @DipanshKhandelwal Dear Sir, I have already created an user in firebase console, and in app component i do have a login form and from that component, using "signInWithEmailAndPassword", im logging into app component. And in CreateUser component im creating more users. But it's making me logout – MANOJ H M Jul 1 at 6:34
  • How did you determine that the user is logged out after running this code? It typically helps if you can include a console.log() statement in your code that shows where you're not getting the value you expect. – Frank van Puffelen Jul 1 at 14:24
  • @FrankvanPuffelen Sir the app is taking me to login screen again. Thats how i determined that its logging me out – MANOJ H M Jul 1 at 15:56
  • Keep in mind that we can't see your screen, and I can't quickly see from your code how that navigation works. I highly recommend logging the values that your navigation depends on, and sharing the updated code and its output in your question. Also see how to create a minimal, complete, verifiable example, as this is by far the best way to increase the odds that someone can help. – Frank van Puffelen Jul 1 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.