Miss any of our Open RFC calls?Watch the recordings here! »

just-login-client

2.0.1 • Public • Published

just-login-client

Build Status

Example

Create a server:

var JustLoginCore = require('just-login-core')
var JustLoginSessionManager = require('just-login-example-session-manager')
var http = require('http')
var level = require('level')
var shoe = require('shoe')
var dnode = require('dnode')
 
var db = level('./databases/core')
var core = JustLoginCore(db)
var sessionManager = JustLoginSessionManager(core)
var server = http.createServer()
 
shoe(function(stream) {
    var d = dnode(sessionManager)
    d.pipe(stream).pipe(d)
}).install(server, '/dnode-example')

Create a client:

var justLoginClient = require('just-login-client')
 
var client = justLoginClient('/dnode-example', function (err, newApi, sessionId) {
    if (!err) {
        //do stuff with the api
    }
})
 
client.on('session', function (session) {
    if (session.continued) {
        console.log('Reusing my session:', session.sessionId)
    } else {
        console.log('New session:', session.sessionId)
    }
})
client.on('authenticated', function (email) {
    console.log(email + ' just got logged in!')
})

API

var client = require('just-login-client')

Server Side

var sock = client(core, sessionState)

  • core is a just-login-core object.
  • sessionState is a just-login-session-state object.
  • Returns a sock object.
var sock = client(core, sessionState)
sock.install(http.createServer(), '/dnode')

Client Side

var emitter = client(dnodeEndpoint, cb)

This function handles remembering the session id in the browser's local storage.

  • dnodeEndpoint is a string for the endpoint that dnode uses for communication. The string must start with a forward slash /.
  • cb is a function that has the following arguments:
    • err is either null or an Error object.
    • newApi is documented here.
    • sessionId is the new (or previous, when applicable) session id.
  • Returns emitter which can emit the following events:
    • session is emitted when a session is initiated. An object is emitted with the following properties:
      • sessionId The id for the current session. E.g. 3879533a-1f34-11e4-a8de-c92c3319c4e0
      • continued Whether or not the session was continued from a previous session. E.g. true, false
    • authenticated is emitted when the user gets authenticated.
      • email is the email of the user who logged in. E.g. you@youremail.com
emitter.on('session', function (data) {
    console.log(data.continued) //boolean for if the session was continued or newly created
    console.log(data.sessionId) //string for the session id
})
 
emitter.on('authenticated', function (whom) {
    t.ok(whom, 'got authenticated')
    t.equal(whom, fakeEmailAddress, 'correct email (new)')
})

Install

Install with npm

npm install just-login-client

License

VOL

Keywords