如何为登录系统创建Web服务? [英] How to create a webservice for a login system?
问题描述
我想知道是否可以使用Web服务创建登录系统,因为我需要在Smartface App Studio中的应用程序中添加登录系统?
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?
谢谢
推荐答案
是的,有可能.
一种可能的解决方案是从服务器获取用户信息,并比较用户键入的密码和来自服务器的密码(如果相等),请登录.我个人使用Firebase作为服务器,因此我保存了用户的密码.对象名称作为他的电子邮件,因此每次我想要获取该用户的对象时,我都会使用URL中的他的电子邮件发出GET请求.
例如
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!");
}
}
});
希望有帮助! :)
编辑
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)
,其中callback是要在登录完成后运行的函数.并且,当您要注册用户时,请使用firebase.register(someEmail, somePassword, callback)
.
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.只要记住要在Firebase规则中更改auth值即可.
OBS. Just remember to change the auth value in Firebase rules.
这篇关于如何为登录系统创建Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!