I am using firebase as a Web client for an app to create a login system however when I enter a username & password & press login it say server error

127 views Asked by At

I am using firebase as a Web client for an app to create a login system however when I enter a username & password & press login it say server error.

How do I resolve the server error and what does it mean by server error?

Thank you and help would be appreciatedâ˜ș The code I used to create the Web client

var ROOT_URL = "https://exampleapp.firebaseio.com/"; // Change to your Firebase App 
var FIREBASE_CREDENTIAL = "yourAppSecret"; // Change to your Firebase App Secret
var firebase = { 
    register : function (email, password, callback) { 
        var emailReplace = email.replace(/\./g, ",");
        var beginRegister = function () {
            var requestObj = { "email" : email, "password" : password };
            var requestJSON = JSON.stringify(requestObj);
            var wcRegister = new SMF.Net.WebClient({
                URL : ROOT_URL + "Users/" + emailReplace + ".json?auth=" + FIREBASE_CREDENTIAL,
                httpMethod : "POST",
                requestHeaders : ['Content-Type:application/json', 'X-HTTP-Method-Override:PATCH'],
                requestBody : requestJSON,
                onSyndicationSuccess : function (e) {
                    alert("Begin register success");
                },
                onServerError : function (e) {
                    alert("Begin register error");
                } 
            });
            wcRegister.run(true);
        }; 
        var isTaken = new SMF.Net.WebClient({
            URL : ROOT_URL + "Users/" + emailReplace + ".json?auth=" + FIREBASE_CREDENTIAL,
            httpMethod : "GET",
            requestHeaders : ["Content-Type:application/json"],
            onSyndicationSuccess : function (e) {
                alert("Is taken sucess");
                var response = JSON.parse(isTaken.responseText);
                if (response !== null) {
                    //Email is taken, do something
                } else { 
                    beginRegister(); //Email is not taken, continue 
                }
            }, onServerError : function (e) { 
                //Server Error, do something
                alert("Is taken error");
            }
        });
        isTaken.run();
    }
};

I am using smartface app studio.

2

There are 2 answers

0
halit On

Server error means that your request to given URL couldn't successfully made. I have tested your code and it worked for me with my own firebase configurations.

If you are using custom certificate file, which located in Assets folder of your project and named 'cacert.pem', maybe that CA don't accept that domain I don't know. You can check error message in onServerError with

alert(e.message);

This will give more detailed information about error. Also did you test url and body with another HTTP request tool like hurl.it?

0
Luccas Clezar On

Well, I know it's working because I wrote this code :P but probably it's because you didn't change your Firebase rules to access with your app secret or you didn't properly add the variables values. Did you change FIREBASE_CREDENTIAL to your app secret? Did you change ROOT_URL to the root url of your Firebase app?

If you did both, check your firebase rules, it should be something like:

{
    "rules": {
        ".read": "auth == 'YOUR_APP_SECRET'",
        ".write": "auth == 'YOUR_APP_SECRET'"
    }
}

Remember to change YOUR_APP_SECRET with your app secret (pretty logical, huh :P).

If you already done that and still it doesn't work, check if you are calling the function right, and before trying to log in an user, register him. By the way, the code that you showed is incomplete, it doesn't have the login function, this could be the problem too.

And just to you know, if you want to add more info about the user, you can modify the functions to add as many parameters as you want, but this would be another question right? :P