Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering and login users in Azure Mobile Services

I was following this series about Mobile Services and I am using examples in latest tutorial. Now I want to impelement registering and login in Windows Phone for example. I changed to Insert permission to anyone with application key and I can Insert new user by this code:

await accountTable.InsertAsync(new accounts() { Username = "admin", Password = "mypassword" });

But I don't know how can I now check for login user? How to get token?

like image 563
Libor Zapletal Avatar asked Sep 19 '13 11:09

Libor Zapletal


1 Answers

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 });
        }
    });
}
like image 181
carlosfigueira Avatar answered Sep 23 '22 09:09

carlosfigueira