Skip to content
Advertisement

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.

I’m able to create a new user with email and password.

But when email already exists in the database or password is less than 6 characters, it doesn’t show the proper errors.

It just returns 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 400n    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!

Advertisement

Answer

By default firebase doesn’t return response in the error body but it can be extracted with.

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

This is not mentioned in the official firebase docs.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement