1

I was wondering if it was possible to create a login system using a webservice as I need to add a login system in my app in Smartface App Studio?

Thanks

1 Answers1

0



Yes, it's possible.
One of the possible solutions is to get the user's information from a server and compare the password that the user typed and the one that came from the server, if it's equal, log in. I personally use Firebase as my server, so I save user's object name as his email, so every time I want to get this user's object, I make a GET request with his email in the URL.
Eg.

var webclient = new SMF.Net.WebClient({
  URL : "https://exampleapp.firebaseio.com/Users/example@example.com",
  httpMethod : "GET",
  ...
  onSyndicationSuccess : function(e){
    var response = JSON.parse(e.responseText);
    if(response.password === Pages.Page1.UserPassword.text){
      //Login
    } else {
      alert("Wrong Password!");
    }
  }
});

Hope that helps! :)


EDIT

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 () {
            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) {
                        //Registered, do something
                        callback();
                    },
                    onServerError : function (e) {
                        //Do something
                    }
                });
            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) {
                    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
                }
            });
        isTaken.run(true);
    },
    login : function (email, password, callback) {
        var emailReplace = email.replace(/\./g, "%2C");
        var wcLogin = new SMF.Net.WebClient({
                URL : ROOT_URL + "Users/" + emailReplace + ".json?auth=" + FIREBASE_CREDENTIAL,
                httpMethod : "GET",
                requestHeaders : ["Content-Type:application/json"],
                onSyndicationSuccess : function (e) {
                    var responseText = JSON.parse(wcLogin.responseText);
                    if (responseText) {
                        if (password === responseText.password) {
                            //User logged, do something
                            callback();
                        } else {
                            //Password is wrong, do something
                        }
                    } else {
                        //User doesn't exist, do something
                    }
                },
                onServerError : function (e) {
                    //Server error, do something
                }
            });
        wcLogin.run(true);
    }
  }

Put this code somewhere in global scope (in an empty space) and when you want the user to login, use firebase.login(someEmail, somePassword, callback), where callback is a function that you want to run when the login is finished. And when you want to register a user, use firebase.register(someEmail, somePassword, callback).

OBS. Just remember to change the auth value in Firebase rules.

Luccas Clezar
  • 953
  • 10
  • 16
  • Well, using Firebase REST API, you can't use Firebase's Simple Auth or any other auth methods. So, you could implement a auth method yourself using firebase's custom auth, but I can't help you with that, sorry. What I did was just add a rule in Firebase that prevents any call of reading (or writing) anything to the server without the app secret. To do that, just go to your app rules and update .read and .write methods with `"auth = [YourAppSecret]"` (app secret can be found in secrets tab). And now, every time you make a call to the server, add `?auth=YourAppSecret` in the end of the URL. – Luccas Clezar Apr 05 '16 at 13:18
  • Your URL would be something like: `https://exampleapp.firebaseio.com/Users/someUser.json?auth=yourAppSecret` – Luccas Clezar Apr 05 '16 at 13:22
  • I used my Firebase url as a webclient and I'm not sure how to use the client for a login sytem – Lucy Matthew Apr 05 '16 at 18:17
  • I posted an example in my answer. You have to GET the information from the user's path (in Firebase), when the server returns it, you have to compare the information that the user wrote (email and password) with the one that came from the server. Of course, you need user's email to do that, so just put the URL as a variable, like `URL: "https://exampleapp.firebaseio.com/Users" + userEmail + ".json?auth=YourAppSecret"` . If you still don't know how to do it, let me know and I'll update my answer with a complete example. :) – Luccas Clezar Apr 06 '16 at 15:09
  • https://docs.google.com/drawings/d/1SHU6I6othvOvscCAbzkwXVwpy_3rs7XU76PIJg9ZKbA/edit?usp=sharing – Lucy Matthew Apr 08 '16 at 16:58
  • This is what happens when I click next once I've adding the web feed URL↑ – Lucy Matthew Apr 08 '16 at 17:01
  • First of all, I recommend that you create a webClient by code, because the static WebClient doesn't accept a variable as any parameter. I personnaly use it only for testing. I'll edit my answer to give a full example :) – Luccas Clezar Apr 09 '16 at 14:44
  • https://docs.google.com/drawings/d/16xHkEhUZoM0Zs_DFFmJ1z3JVEaFetYICWVtvfiIr7kA/edit?usp=sharing – Lucy Matthew Apr 11 '16 at 15:27
  • Yep, this is what I meant :) – Luccas Clezar Apr 11 '16 at 18:03
  • Now how do I use the Web client to create a login system – Lucy Matthew Apr 12 '16 at 05:12
  • Just create a login and a register screen with email and password (you can implement more fields if you want) and add these field's text as parameters of the firebase function. E.g. `firebase.login(Pages.Page1.emailEditBox, Pages.Page1.passwordEditBox, function() { alert("User logged!") });` – Luccas Clezar Apr 12 '16 at 17:47
  • https://docs.google.com/drawings/d/1eJNyKCBjFu1s1TaHcdilIPCqEaPbS1pKJ3A7_iRKGNg/edit?usp=sharing – Lucy Matthew Apr 12 '16 at 20:05
  • Is this how your supposed to do it. – Lucy Matthew Apr 12 '16 at 20:05
  • First of all, there are some inconsistencies in your code... But I will focus on the Firebase part. So, this is not the right approach, you can't login every time you change the text of the editbox. Create a button and in the onPressed event of this button take the text from your two EditBox'es and run the firebase.login method with these texts. – Luccas Clezar Apr 12 '16 at 20:41
  • I am really struggling so is it possible if you could create a simple project which has a login system. Thank you I really appreciate this – Lucy Matthew Apr 13 '16 at 14:59
  • It's quite simple but it's here: https://mega.nz/#F!mpYHUYrD!EfJ-zUHpQMHT9j1jFq3K0A :) – Luccas Clezar Apr 13 '16 at 23:52
  • https://docs.google.com/drawings/d/1XOibcUQviuftMl6X4o_qsowlEmu_EjDLpaDK7jQinQc/edit?usp=sharing – Lucy Matthew Apr 14 '16 at 15:55
  • I tried to open the project however when I did this happened ↑ – Lucy Matthew Apr 14 '16 at 15:56
  • I think it's an issue with Mega... I'll update the file. – Luccas Clezar Apr 15 '16 at 13:24
  • How do I add the firebase server to this as when I tested it it said – Lucy Matthew Apr 16 '16 at 14:01
  • https://docs.google.com/drawings/d/1l3kNQKAQ28iw6diXXaM-2-fSmNW0BXoecZqb18w0Av8/edit?usp=sharing – Lucy Matthew Apr 16 '16 at 14:01
  • Could be a lot of things... Did you update your firebase rules? Did you change the credentials and root url variables? – Luccas Clezar Apr 16 '16 at 22:20
  • https://docs.google.com/drawings/d/1ya8_GXe-VQvOAoGFqBHH_8H1H7WzNCBO2eKoh-opU-0/edit?usp=sharing – Lucy Matthew Apr 17 '16 at 19:15
  • Ok, and did you change the rules on Firebase? – Luccas Clezar Apr 18 '16 at 15:24
  • What do you change the rules to? – Lucy Matthew Apr 18 '16 at 15:36
  • Change both .write and .read values to "auth == yourAppSecret" (change yourAppSecret with your Firebase app secret. – Luccas Clezar Apr 18 '16 at 16:18
  • https://docs.google.com/drawings/d/15moJiuZzjRiB3wbYE7F3Wdintx4fjVKli85JC4L9GTA/edit?usp=sharing – Lucy Matthew Apr 18 '16 at 16:29
  • Delete everything inside rules. Then add the following: `".write": "auth == yourAppSecret", ".read": "auth == yourAppSecret"`. Again, make sure you replace yourAppSecret with Firebase's Secret – Luccas Clezar Apr 20 '16 at 13:38
  • https://docs.google.com/drawings/d/1KoOhe0QIYSaotg16HP9t7_5CtyVPMlzMCMbHeDwCybI/edit?usp=sharing – Lucy Matthew Apr 20 '16 at 16:43
  • How do you correct the code to make the error solved? – Lucy Matthew Apr 20 '16 at 16:45
  • Your app secret must come between apostrophe. E.g. `"auth == 'yourAppSecret'"` – Luccas Clezar Apr 20 '16 at 21:55
  • Is the login and authentication custom authentication or email and password? – Lucy Matthew May 08 '16 at 14:53
  • Try the app on the device emulator to see the problem and I used the same code in which you used on the sample app you gave me and sorry for the late reply. – Lucy Matthew May 09 '16 at 17:31
  • I'm so sorry for taking a long time to answer, but I didn't see the comment. I think that if you read my answer on your last question, you'll get the solution :) – Luccas Clezar May 25 '16 at 15:28