Windows Azure Mobiles Services implements login registration for clients

Source: Internet
Author: User

The following is a simple implementation, with the client using the Android implementation as an example:

User table account:

Package Com.microsoft.ecodrive.model;public class Account {@com. Google.gson.annotations.SerializedName ("id") public string id; @com. Google.gson.annotations.SerializedName ("username") Public String username;@ Com.google.gson.annotations.SerializedName ("password") public String password; @Overridepublic boolean equals (Object O) {return o instanceof account && ((account) o). id = = ID;}}

First, login

1, the server new Api:login, the script is replaced by the following:

Exports.post = function (Request, response) {    //use ' Request.service ' to access features of your mobile service, e.g.:    //   var tables = request.service.tables;   var push = Request.service.push;    Response.send (Statuscodes.ok, {message: "POST"}); Exports.get = function (Request, response) {    var myTable = request.service.tables.getTable (' account ');    Mytable.where ({        username:request.param (' username '),        password:request.param (' Password ')    }). Read ({        success:checkpermissions    });    function Checkpermissions (results) {        if (results.length <= 0) {            response.send (statuscodes.bad_request, ' Not such user. ');        } else {            Response.send (Statuscodes.ok, {message: ' sucess. '});}}    ;

2, the client The login code is as follows:

private void Login (final string name,final string pwd) {list<pair<string, string>> parameters = new arraylist& Lt Pair<string, string>> ();p arameters.add (New pair<string, string> ("username", name));p Arameters.add ( New pair<string, string> ("password", pwd); MCLIENT.INVOKEAPI ("Login", "get", parameters, Apiresult.class, new Apioperationcallback<apiresult> () {        @Override public        void oncompleted (apiresult result, Exception Exception, servicefilterresponse response) {        int code = response.getstatus (). Getstatuscode (); LOG.I (TAG, "StatusCode:" +code); if (exception = = null) {showtoast ("Login sucess!");} else if (code==400) {showtoast ("Username or password is wrong, please try again."); Else{showtoast (Exception.getmessage ());}        }    );}
Note: Because it is taken from the project, some methods need to initialize themselves.

Second, registration

Method One: API mode

1, the server new Api:register, the script is replaced by the following:

exports.post    = function (Request, response) {var myTable = request.service.tables.getTable (' account ');    Mytable.where ({username:request.param (' username ')}). Read ({success:checkpermissions}); function Checkpermissions (results) {if (results.length <= 0) {var toinsert ={username:request.param            (' username '), password:request.param (' Password ')}; Mytable.insert (Toinsert, {success:function () {response.send (Statuscodes.ok, {MESSAG                E: ' Register sucess! '});        }            });            } else {Console.log (' User%s already exist. ', Request.param (' username '));        Response.send (statuscodes.bad_request, ' already exist. '); }}};exports.get = function (Request, response) {Response.send (Statuscodes.ok, {message: ' Hello world! '}); 

2, the client The code is as follows:

Use apiprivate void Register1 (final string name,final string pwd) {list<pair<string, string>> parameters = N EW arraylist<pair<string, string>> ();p arameters.add (New pair<string, string> ("username", name)); Parameters.Add (New pair<string, string> ("password", pwd)), Mclient.invokeapi ("register", "POST", parameters, Apiresult.class, New apioperationcallback<apiresult> () {        @Override public        void oncompleted (Apiresult result, Exception Exception, servicefilterresponse response) {        int code = response.getstatus (). Getstatuscode (); LOG.I (TAG, "StatusCode:" +code); if (exception = = null) {//sucessshowtoast (result.message);} else if (code==400) {showtoast (response.getcontent ());} Else{showtoast (Exception.getmessage ());}        }    );}

Method Two: Modify the Insert script for the Accout table.

1. Insert script to replace the Accout table is as follows:

function Insert (item, user, request) {    var permissionstable = tables.gettable (' account ');    Permissionstable.where ({        username:item.username    }). Read ({        success:checkpermissions    });    function Checkpermissions (results) {        if (results.length <= 0) {            request.execute ();        } else {            Console.log (' User%s already exist. ', item.username);            Request.respond (Statuscodes.bad_request, ' User already exist ');}}}    

< Span style= "font-family: ' Comic Sans MS '; font-size:14px "> 2, client code as follows:

< Span style= "font-family: ' Comic Sans MS '; font-size:14px ">

Use insertprivate void Register (final String name,final string pwd) {Account account = new account (); account.username = Name;account.password = Pwd;maccounttable.insert (account,new tableoperationcallback<account> () {@ overridepublic void oncompleted (account result, Exception exception,servicefilterresponse response) {INT code = response . GetStatus (). Getstatuscode (); LOG.I (TAG, "StatusCode:" +code); if (exception = = null) {//sucessshowtoast ("Register sucess!");} else if (code==400) {showtoast (Response.getcontent (). replace ("\" "," "));} Else{showtoast (Exception.getmessage ());}});}

In addition, the Apiresult class is as follows:

Package Com.microsoft.ecodrive.model;import Com.google.gson.annotations.serializedname;public class APIResult {@ Serializedname ("Count") public    int mCount;    public int GetCount () {return mCount;} public String message;


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.