/passport-habrahabr

Habrahabr authentication strategy for Passport and Node.js

Primary LanguageJavaScriptMIT LicenseMIT

passport-habrahabr

Passport strategy for authenticating with Habrahabr using the OAuth 2.0 API.

This module lets you authenticate using Habrahabr in your Node.js applications. By plugging into Passport, Habrahabr authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

Install

$ npm install passport-habrahabr

Usage

Configure Strategy

The Habrahabr authentication strategy authenticates users using a Habrahabr account and OAuth 2.0 tokens. The strategy requires a verify callback, which accepts these credentials and calls done providing a user, as well as options specifying an app ID, app secret and callback URL.

passport.use(new HabrahabrStrategy({
    clientID: HABRAHABR_APP_ID,
    clientSecret: HABRAHABR_APP_SECRET,
    callbackURL: "http://localhost:3000/auth/habrahabr/callback"
  },
  function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({ habrahabrId: profile.id }, function (err, user) {
      return done(err, user);
    });
  }
));

Authenticate Requests

Use passport.authenticate(), specifying the 'habrahabr' strategy, to authenticate requests.

For example, as route middleware in an Express application:

app.get('/auth/habrahabr',
  passport.authenticate('habrahabr'));

app.get('/auth/habrahabr/callback',
  passport.authenticate('habrahabr', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

Credits

License

The MIT License

Copyright (c) 2011-2014 Jared Hanson http://jaredhanson.net/

Copyright (c) 2014 kafeman http://habrahabr.ru/users/kafeman/