Handles tokens for just-login.
var JustLoginCore = require('just-login-core')
var db = require('level')('./databases/core')
var core = JustLoginCore(db)
// From a form on the website:
// http://example.com/login?sessionId=abcdefab-abcd-abcd-abcdefabcdef&email=joe@example.com
router.get('/login', function (req, res) {
var query = url.parse(req.url, true).query
var successHtml = '<p>U shud receiv email within few minutez...</p>'
core.beginAuthentication(query.sessionId, query.email, sendResponse(res, successHtml))
})
// From the link in the email:
// http://example.com/authenticate?token=12345678-1234-1234-123456789012
router.get('/authenticate', function (req, res) {
var query = url.parse(req.url, true).query
core.authenticate(query.token, sendResponse(res, '<p>U r nao loggd in!!!</p>'))
})
function sendResponse(res, successHtml) {
return function onRequest(err) {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' })
res.end(err.message)
} else {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(successHtml)
}
}
}var Core = require('just-login-core')dbis expecting a levelup database.optionsis an object that holds the (gasp) options!tokenGeneratoris expecting a function that returns an unique string each time it is called. This is used for token generation. Defaults to a UUID generator.tokenTtlis a number in milliseconds of a token's Time To Live (TTL). Defaults to 5 minutes.tokenTtlCheckIntervalMsis a number in milliseconds of the ttl's check interval. (See tiny-level-ttl,checkInterval.) Defaults to 10 seconds.
- Returns
core.
It emits some events and has a few methods:
Starts the authentication process by emitting the 'authentication initiated' event with a token and the contact address.
Something else must listen for the event, and send a message to the user. See core events for more information.
sessionIdis a string of the session id that is trying to get authenticated.contactAddressis string of the user's contact info, (usually an email address).cbis a function with the following arguments:erris an Error object or null.authReqInfois an object with the authentication request information (or null if an error occurred). The object is identical to the object emitted in the event, with the following properties:contactAddressis a string with the contact address.tokenis a string of the token.
Emits core.on('authentication initiated', function (authReqInfo) { ... })
core.beginAuthentication('session id', 'fake@example.com', function (err, authReqInfo) {
if (!err) {
console.log(authReqInfo.token) //logs the token
console.log(authReqInfo.contactAddress) //logs: "fake@example.com"
}
})Authenticates the token, and calls back with the session id and contact address associated with that token. Then the token and it's associated data is deleted. A token can only be authenticated once.
tokenis a string of the token that is trying to get authenticated.cbis a function with the following arguments:erris an Error object or null.credentialsis null is the user is not authenticated, and is an object if they are authenticated:contactAddressis a string of their contact address.sessionIdis a string of their session id.
Emits core.on('authenticated', function (credentials) { ... })
core.authenticate('the token', function(err, credentials) {
if (!err) {
console.log(credentials.contactAddress + ' is now logged in! Congratulations!')
} else {
console.log('Sorry, for some reason you are not logged in.')
}
})Emitted when beginAuthentication() is called. (Which should be when the user clicks the "login" button.)
core.on('authentication initiated', function (authReqInfo) {
console.log(authReqInfo.contactAddress)
console.log(authReqInfo.token)
})(You can use just-login-emailer to catch this event.)
Emitted when core.authenticate() is successful.
core.on('authenticated', function (credentials) {
console.log(credentials.contactAddress)
console.log(credentials.sessionId)
})Install with npm:
npm install just-login-core