Register and register users in Azure Mobile Services

I have been following this series about mobile services and I am using examples in the last tutorial. Now I want to import registration and login into Windows Phone, for example. I changed the β€œPaste” permission to everyone who has the application key, and I can insert a new user by this code:

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

But I do not know how I can now verify the user login? How to get a token?

+6
source share
1 answer

The message you indicated was written at the end of last year when there was no support for user APIs on Azure Mobile Services - the only place where you could run scripts for user calls was on the tables. For now, you should use custom APIs for this - where you can define two APIs - one for registering a user and one for logging in. On the client, when you call the login, the API will check the username / password, and then return the Zumo token (created using the script specified in this 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 will look something like this:

 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 }); } }); } 
+5
source

Source: https://habr.com/ru/post/954158/


All Articles