Registering and login users in Azure Mobile Services

陌路散爱 提交于 2019-12-03 14:41:41

The post you referred was written at the end of last year, when there was no support for custom APIs on Azure Mobile Services - the only place where you could have scripts executed for user calls were on tables. Nowadays you should actually use custom APIs for that - where you can define two APIs - one for registering the user, and another one for login. On the client, when you call login, the API would validate the user name / password, then return the Zumo token (created with the script shown in that blog post), which the client can then set to the CurrentUser property of the MobileServiceClient object.

Something like the code below:

var loginInput = new JObject();
loginInput.Add("userName", "theUserName");
loginInput.Add("password", "thePassword");
var loginResult = await client.InvokeApiAsync("login", loginInput);
client.CurrentUser = new MobileServiceUser((string)loginResult["user"]);
client.CurrentUser.MobileServiceAuthenticationToken = (string)loginResult["token"];

And the API would look something like the code below:

exports.post = function(req, res) {
    var user = req.body.userName;
    var pass = req.body.password;
    validateUserNamePassword(user, pass, function(error, userId, token) {
        if (error) {
            res.send(401, { error: "Unauthorized" });
        } else {
            res.send(200, { user: userId, token: token });
        }
    });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!