简体   繁体   中英

How to use firebase.auth() in firebase-functions?

I'm trying to create a API and i want to use firebase.auth() for a signup endpoint in firebase functions with Express, but i cant find a way to get it working, this is what i have:

const functions = require("firebase-functions");
const admin = require('firebase-admin');
const app = require('express')();
admin.initializeApp();
const config = {
  //key
};

const db = admin.firestore();

const firebase = require('firebase/app');
firebase.initializeApp(config);
require('firebase/auth')



//singup route
app.post('/signup', (req, res) =>{
  const newUser = {
    email: req.body.email,
    password: req.body.password,
    confirmPass : req.body.password,
    nickName: req.body.nickName
  }
  //todo validate data

  firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password)
    .then(data => {
      return res.status(200).json({message: `user ${data.user.uid} sign up siccesfully`});
    })
    .catch(err => {
      console.error(err);
      return res.status(500).json({error : err.code});
    })
});

Exist a way to acomplish this?

Notes : i tried using import, but firebase-functions doesnt work.

When i do a request in postman i get this error:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <pre>TypeError: firebase.auth is not a function<br> &nbsp; &nbsp;at 

And nothing happend in firebase, then i want to be hable to create a user using my API, and return the token

move

require('firebase/auth')

above

firebase.initializeApp(config);

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