简体   繁体   中英

How do i input database in firestore with user's input for name of the document?

I have a problem with my api. I want to use the firestore to input data for login & register with APi. Then, when i want to add the document database with user's input email when register but the output is error. my reasons for adding document with user's email is for easier for the next login step for its validation to compare the password also the email. here the code that i already make:

const functions = require("firebase-functions");
const express = require("express");
// eslint-disable-next-line no-unused-vars
const cors = require("cors");

const admin = require("firebase-admin");
admin.initializeApp();

const app = express();
const db = admin.firestore();

app.post("/Register", (req, res) => {
  const {
    name,
    email,
    password,
    phone,
  } = req.body;
  const saldo = 0;
  const valAcc = db.collection("Users").doc(email);
  valAcc.get().then((doc) => {
    if (doc.exists) {
      res.status(409).send("User Already Exist!");
    }
  });
  db.collection("Users").doc(email).add({
    Name: name,
    Email: email,
    Password: password,
    Phone: phone,
    Saldo: saldo,
  });
  res.status(201).send();
});

exports.apptest = functions.https.onRequest(app);

Do you guys have any solution? rly appriciate for any help.

I don't recommend handling user authentication yourself. I recommend using Firebase built-in tool for this instead: Firebase Authentication

With this, it's easy to implement email/passwords or providers (google, facebook etc) login.

And it's probably much safer than doing from scratch.

Afterwards you can use the UID or email of the logged user for storing/retrieving data on Firestore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM