Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
112 lines (79 sloc) 2.71 KB

Derby User Providers

Add 3rd party providers to derby-user such as facebook, google, twitter, etc…


$ npm install derby-user-providers --save

Server Usage

In your server file, add the middleware:

var providers = require('derby-user-providers');

var google = {
  config: {
    clientID: '...',
    clientSecret: '...',
    strategy: {
      module: 'passport-google-oauth',
      name: 'OAuth2Strategy'

var facebook = {
  config: {
    clientID: '...',
    clientSecret: '...',
    authOptions: {scope: ['email']},
    strategy: require('passport-facebook').Strategy

  // ...
  // ...
  // cookieParser, session, transport, model, bodyParser...
  // ...
  // ...
  .use('/user', providers({
    origin: 'http://localhost',
    path: '/user',
    providers: {facebook: facebook, google: google}

App Usage

In your view:

<a href="/user/auth/facebook">Sign in with Facebook</a>
<a href="/user/auth/google">Sign in with Google+</a>

Handling events

You can handle events.

var events = require('events');
var emitter = new events.EventEmitter();

emitter.on('user.auth', function (req, data) {
  var model = req.getModel();
  console.log('user ' + data.userId + ' signed in.');

  // ...
  // ...
    emitter: emitter, ...


/auth/:provider?params – Call this route to authenticate a user. The params are merged into authOptions.

/auth/:provider/done – This route is called when authentication is done. Can be overriden. Redirects to / by default.


originRequired. The site origin providers will redirect back to upon sign in.

emitter – Specify an event emitter that will handle events.

path – Must be set if you use an express path prefix. (See above)

providers – An object containing providers and their configuration settings.


config – Configuration options for the strategy. Refer to the passport strategy's docs.

strategy – The passport strategy function or an object with the strategy name and/or module.

authOptions – Passport authenticate options passed in during verification. Used to pass in scope, etc.

callbackOptions – Passport authenticate options passed in during the callback.


user.auth(req, data) – Event emitted when a new user authenticates. Contains userId and provider name.