npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

passport-vk-strategy

v1.0.0

Published

passport vk strategy

Downloads

111

Readme

passport-vk-strategy

Build Status NPM Version

Passport strategy for authenticating with VK.com using the OAuth 2.0 API.

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

Installation

$ npm install passport-vk-strategy

Usage

Configure Strategy

The VK.com authentication strategy authenticates users using a VK.com 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 a app ID, app secret, and callback URL.

const VKontakteStrategy = require('passport-vk-strategy').Strategy;

// User session support middlewares. Your exact suite might vary depending on your app's needs.
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({extended: true}));
app.use(require('express-session')({secret:'keyboard cat', resave: true, saveUninitialized: true}));
app.use(passport.initialize());
app.use(passport.session());

passport.use(new VKontakteStrategy(
  {
    clientID:     VKONTAKTE_APP_ID, // VK.com docs call it 'API ID', 'app_id', 'api_id', 'client_id' or 'apiId'
    clientSecret: VKONTAKTE_APP_SECRET,
    callbackURL:  "http://localhost:3000/auth/vkontakte/callback"
  },
  function myVerifyCallbackFn(accessToken, refreshToken, params, profile, done) {

    // Now that we have user's `profile` as seen by VK, we can
    // use it to find corresponding database records on our side.
    // Also we have user's `params` that contains email address (if set in 
    // scope), token lifetime, etc.
    // Here, we have a hypothetical `User` class which does what it says.
    User.findOrCreate({ vkontakteId: profile.id })
        .then(function (user) { done(null, user); })
        .catch(done);
  }
));

// User session support for our hypothetical `user` objects.
passport.serializeUser(function(user, done) {
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    User.findById(id)
        .then(function (user) { done(null, user); })
        .catch(done);
});

Authenticate Requests

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

For example, as route middleware in an Express application:

//This function will pass callback, scope and request new token
app.get('/auth/vkontakte', passport.authenticate('vkontakte'));

app.get('/auth/vkontakte/callback',
  passport.authenticate('vkontakte', {
    successRedirect: '/',
    failureRedirect: '/login' 
  })
);

app.get('/', function(req, res) {
    //Here you have an access to req.user
    res.json(req.user);
});
Display Mode

Set display in passport.authenticate() options to specify display mode. Refer to the OAuth dialog documentation for information on its usage.

app.get('/auth/vkontakte',
  passport.authenticate('vkontakte', { display: 'mobile' }),
  function(req, res){
    // ...
  });

Extended Permissions

If you need extended permissions from the user, the permissions can be requested via the scope option to passport.authenticate().

For example, this authorization requests permission to the user's friends:

app.get('/auth/vkontakte',
  passport.authenticate('vkontakte', { scope: ['status', 'email', 'friends', 'notify'] }),
  function(req, res){
    // The request will be redirected to vk.com for authentication, with
    // extended permissions.
  });

Profile Fields

The VK.com profile may contain a lot of information. The strategy can be configured with a profileFields parameter which specifies a list of additional fields your application needs. For example, to fetch users's city and bdate configure strategy like this.

Notice that requesting the user's email address requires an email access scope, which you should explicitly list as in following example:

passport.use(new VKontakteStrategy(
  {
    // clientID: ..., clientSecret: ..., callbackURL: ...,
    scope: ['email' /* ... and others, if needed */]
    profileFields: ['email', 'city', 'bdate']
  },
  myVerifyCallbackFn
));

Profile fields language

By default, profile fields such as name are returned in English. The strategy can be configured with a lang parameter which specifies language of value returned.

For example, this would configure the strategy to return name in Russian:

passport.use(new VkontakteStrategy(
  {
    // clientID: ..., clientSecret: ..., callbackURL: ...,
    lang: 'ru'
  },
  myVerifyCallbackFn
));

API version

The VK.com profile structure can differ from one API version to another. The specific version to use can be configured with a apiVersion parameter. The default is 5.0.

passport.use(new VKontakteStrategy(
  {
    // clientID: ..., clientSecret: ..., callbackURL: ...,
    apiVersion: '5.17'
  },
  myVerifyCallbackFn
));

Tests

$ npm install --dev
$ make test

Credits

Special Thanks

To all the people who had to cope with idiosyncrasies of OAuth2 and VK API!

License

(The MIT License)

Copyright (c) 2011 Jared Hanson

Copyright (c) 2012, 2016 Stepan Stolyarov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.