gandalf
node.js oauth login wizard with leveldb session storage
install
$ npm install gandalf
usage
var http = var Router = var mount = var level = var Gandalf = var ecstatic = var db = var gandalf = var router = // mount the OAuth login handlersrouter // get the current session datarouter // only logged in people can see this routerouter // these are served without hitting the sessionrouter var server = http server
api
var gandalf = Gandalf(db, options)
create a new authentication handler by passing in an existing leveldb - this can also be a sub-level
Pass a providers
config to activate external OAUTH providers:
var gandalf =
The supported types are:
- github
- dropbox
Password mode (/register and /login) are activated automatically.
One user can connect
multiple external providers and link them to the same user id.
gandalf.session()
Return a function that will create a session
property of the request:
var session = gandalf router
You can also pass a function to gandalf.session and it will be wrapped:
router
gandalf.handler()
Mount the authentication handler onto an endpoint of your application (for example on /auth
).
You can then link to /auth/github
to perform a github login or POST to /auth/register
to register new accounts.
router
The following are the routes that are mounted:
GET /PROVIDER
e.g. /google
- this triggers the OAuth login loop for a provider
POST /register
post username and password and other fields to register a new user
POST /login
post username and password fields to login using the password method
POST /claim
post a username to use for a connected user - use this when a user connects using an external service but you still need a username
(as opposed to just an id)
GET /logout
clear the session and redirect to '/'
GET /check?username=
check if the given username exists
GET /status
return a JSON representation of the current session - this includes OAuth tokens
gandalf.protect(function(req, appid, userid, done){})
Return a 403 error if the user is not logged in:
router
If you do not pass a function then there just needs to be a user for access to be granted.
gandalf.delete(id, function(){})
Delete a user and all their details
gandalf.disconnect(id, provider, function(){})
Remove the connection details for 'provider' in the given user
events
gandalf.on('storage:put', function(key, value){})
When a value has been put to the database
gandalf.on('storage:batch', function(key, value){})
When a batch has been sent to the database
gandalf.on('request', function(req){})
A HTTP request has hit the handler
gandalf.on('provider', function(name, req){})
A HTTP request has hit a provider handler
gandalf.on('protect', function(req){})
A resource has been denied in a protect handler
gandalf.on('login', function(username){})
A login request
gandalf.on('register', function(username, body){})
A register request
gandalf.on('connect', function(provider, profile){})
A connect request
gandalf.on('save', function(id, provider, profile){})
A user profile has been created by provider
gandalf.on('claim', function(username){})
A claim request
gandalf.on('logout', function(req){})
A logout request
gandalf.on('log', function(type, message){})
gandalf.on('log:error', function(type, message){})
license
MIT