Create a server:
var db = require('level')('./databases/core')
var core = require('just-login-core')(db)
var sessionState = require('just-login-session-state')(core, db)
var server = require('http').createServer()
var sock = require('justlogin.xyz-client')(core, sessionState)
sock.install(server, '/dnode-example')Create a client:
var justLoginClient = require('justlogin.xyz-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!')
})var client = require('justlogin.xyz-client')coreis ajust-login-coreobject.sessionStateis ajust-login-session-stateobject.- Returns a
sockobject.
var sock = client(core, sessionState)
sock.install(http.createServer(), '/dnode')This function handles remembering the session id in the browser's local storage.
dnodeEndpointis a string for the endpoint that dnode uses for communication. The string must start with a forward slash/.cbis a function that has the following arguments:erris eithernullor anErrorobject.newApiis an object with methods fromjust-login-coreandjust-login-session-state, but with thesessionIdpre-bound:beginAuthentication(contactAddress, [cb])fromjust-login-coreisAuthenticated(cb)fromjust-login-session-stateunauthenticate([cb])fromjust-login-session-statesessionExists(cb)fromjust-login-session-state
sessionIdis the new (or previous, when applicable) session id.
- Returns
emitterwhich can emit the following events:sessionis emitted when a session is initiated. An object is emitted with the following properties:sessionIdThe id for the current session. E.g.3879533a-1f34-11e4-a8de-c92c3319c4e0continuedWhether or not the session was continued from a previous session. E.g.true,false
authenticatedis emitted when the user gets authenticated.emailis the email of the user who logged in. E.g.[email protected]
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 with npm
npm install justlogin.xyz-client