Giter Club home page Giter Club logo

sg-custom-auth's Introduction

sg-custom-auth

Code for Custom Authentication with Couchabse Mobile blog article.

auth.js Node.js-based App Server to authenticate against and OpenLDAP server using the passport-ldapauth package for LDAP authentication.

ldap_data.ldif Information to populate the OpenLDAP database. Contains a single user mobileuser.

The following code snippets highlight the call to the App Server and the Couchbase Lite code to authenticate using the session_id.

Authenticate user with App Server by calling POST /login/

reqBody.put("username", <user supplied username>);
reqBody.put("password", <user supplied password>);

String url = <App Server host>:8080/login;
RequestQueue queue = Volley.newRequestQueue(<context>);
JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(
     Request.Method.POST,
     url,
     reqBody,
     new Response.Listener<JSONObject>() {
          @Override
          public void onResponse(JSONObject response) {
               // get session_id from response
               try {
                    String sessionID = response.getString("session_id");
                    // Store session_id
               } catch (JSONException je) {
                    // Handle exception
               }
          }
     },
     new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
               // authentication failed
          }
     });

queue.add(jsonRequest);

Create one-shot replication

Create one-shot replication using saved session_id and re-authenticate if Sync Gateway session has expired.

URI url = null;
try {
     url = new URI(mSyncGatewayEndpoint);
} catch (URISyntaxException e) {
     e.printStackTrace();
     return;
}

ReplicatorConfiguration config = new ReplicatorConfiguration(database, new URLEndpoint(url));
config.setReplicatorType(ReplicatorConfiguration.ReplicatorType.PUSH_AND_PULL);
config.setContinuous(false);
config.setAuthenticator(new SessionAuthenticator(sessionID));

Replicator replicator = new Replicator(config);
replicator.addChangeListener(new ReplicatorChangeListener() {
     @Override
     public void changed(ReplicatorChange change) {
          CouchbaseLiteException error = change.getStatus().getError();
          if (error != null) {
               if (error.getCode() == 10401) {                          
                    // session expired; re-authenticate
               }
          } 

          ...
     }
});
replicator.start();

sg-custom-auth's People

Contributors

dugbonsai avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.