Windows Azure Mobiles Services implements the client's sign-in brochure

Source: Internet
Author: User

The following is just 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 to create a new api:login, the script to replace such as 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, ' No such user. ');        } else {            Response.send (Statuscodes.ok, {message: ' sucess. '});}}    ;

2. 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 was picked out from the project. Some methods need to be initialized themselves.

Ii. notes

Method One: API mode

1, the service side new Api:register. replace its script with the following example:

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, {message: ' 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. 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: Change the insert script for the Accout table.


1. Replace the Insert script for the Accout table with the following:

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 ');}}}    

2, client code such as the following:

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 ());}});}

Another, Apiresult class such as the following:

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;


Windows Azure Mobiles Services implements the client's sign-in brochure

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.