如何为登录系统创建Web服务?

时间:2016-04-03 16:11:24

标签: smartface.io

我想知道是否可以使用webservice创建登录系统,因为我需要在Smartface App Studio中的应用程序中添加登录系统?

由于

1 个答案:

答案 0 :(得分:0)



是的,这是可能的。
其中一个可能的解决方案是从服务器获取用户的信息,并比较用户键入的密码和来自服务器的密码,如果相等,则登录。我个人使用Firebase作为我的服务器,所以我保存用户的对象名称作为他的电子邮件,因此每次我想获得此用户的对象时,我都会在URL中使用他的电子邮件发出GET请求。
例如

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!");
    }
  }
});

希望有所帮助! :)


修改

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);
    }
  }

将此代码放在全局范围内(在空白区域中),当您希望用户登录时,请使用firebase.login(someEmail, somePassword, callback),其中回调是您希望在登录完成时运行的功能。如果您想注册用户,请使用firebase.register(someEmail, somePassword, callback)

OBS。只需记住在Firebase规则中更改身份验证值。