简体   繁体   English

使用 Firebase REST API 时如何处理注册用户错误

[英]How to handle sign up a user error while using Firebase REST API

I've been trying to sign up a user using Firebase Rest API using Node & Axios HTTP library.我一直在尝试使用 Node & Axios HTTP 库使用 Firebase Rest API 注册用户。

I'm able to create a new user with email and password .我可以使用emailpassword创建一个新用户。

But when email already exists in the database or password is less than 6 characters , it doesn't show the proper errors.但是当数据库中email already existspassword is less than 6 characters ,它不会显示正确的错误。

It just returns Request failed with status code 400它只返回Request failed with status code 400

Here is my code.这是我的代码。

const config = 
    {
        headers:
        {
            'Content-Type': 'application/json',
        },
    }; 


    data = {
        'email': "test@test.com",
        'password': "password123",
        'returnSecureToken': true
    }

    try
    {
        let signup = await axios.post('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=API_KEY',data,config)

        res.json(signup.data);
    }
    catch(err)
    {
        res.json(err);
    }

Error I get.我得到的错误。

{
    "message": "Request failed with status code 400",
    "name": "Error",
    "stack": "Error: Request failed with status code 400\n    at createError (/Users/user/NodeJS/FirebaseAuth/node_modules/axios/lib/core/createError.js:16:15)\n    at settle (/Users/user/NodeJS/FirebaseAuth/node_modules/axios/lib/core/settle.js:17:12)\n    at IncomingMessage.handleStreamEnd (/Users/user/NodeJS/FirebaseAuth/node_modules/axios/lib/adapters/http.js:244:11)\n    at IncomingMessage.emit (events.js:327:22)\n    at endReadableNT (_stream_readable.js:1224:12)\n    at processTicksAndRejections (internal/process/task_queues.js:84:21)",
    "config": {
        "url": "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=API_KEY",
        "method": "post",
        "data": "{\"email\":\"test@test.com\",\"password\":\"password123\",\"returnSecureToken\":true}",
        "headers": {
            "Accept": "application/json, text/plain, */*",
            "Content-Type": "application/json",
            "User-Agent": "axios/0.21.0",
            "Content-Length": 75
        },
        "transformRequest": [
            null
        ],
        "transformResponse": [
            null
        ],
        "timeout": 0,
        "xsrfCookieName": "XSRF-TOKEN",
        "xsrfHeaderName": "X-XSRF-TOKEN",
        "maxContentLength": -1,
        "maxBodyLength": -1
    }
}

Expected error here is : Email already exists.这里的预期错误是:电子邮件已经存在。 But it doesn't return this error.但它不会返回此错误。

What could be the issue here ?这可能是什么问题?

Thanks!谢谢!

By default firebase doesn't return response in the error body but it can be extracted with.默认情况下,firebase 不会在错误正文中返回响应,但可以使用它进行提取。

console.log(err.response.data.error);

This is not mentioned in the official firebase docs.官方 firebase 文档中没有提到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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