auth-vk

1.1.8 • Public • Published

NPM version NPM downloads

AUTH-VK is a powerful Node.js a module that allows you to easily log in to Vkontakte 🚀

📖 Documentation 🤖 Author

Features

  • 100% coverage of the VKontakte API
  • Predictable abstraction
  • Works with large collections of data
  • Easy authorization form

Installation

Node.js 12.0.0 or newer is required

NPM

npm i auth-vk --save
npm i passport --save

Interaction with the library

  • After installing the library, you will need the following code:
// Example of using the library:
new auth(
  {
    clientID: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET,
    callbackURL: process.env.CALLBACK_URL,
    scope: process.env.SCOPE,
    profileFields: process.env.PROFILE_FIELDS,
  },
  async function verify(accessToken, refreshToken, params, profile, done) {
    process.nextTick(function () {
      done(null, profile);
    });
  }
);
  • You will need to create an application:

  • After creating the app, go to settings and you will need:

ID приложения
Защищённый ключ
clientID: 7624701,
clientSecret: 'xZUHQ8vgnMk4okBAKn1e',
  • Next, you need to specify a link to your site for this example:
callbackURL: "https://dev-up.ru/auth/vk/callback",

Then you need to specify the application rights and display the rights:
You can get a list of rights here 📖 Application Rights.
In the example, we will specify the rights to: Access at any time, Groups, Email, Friends List.

      scope: ["offline", "groups", "email", "friends"],
      profileFields: ["offline", "groups", "email", "friends"],

That's it!

Example usage

Sample code:

const auth = require("auth-vk").Zeuvs;
const express = require("express");
const passport = require("passport");
const expressSession = require("express-session");
const app = express();
require("http").Server(app).listen(80);
app.set("views", __dirname + "/scr");
app.set("view engine", "ejs");

app.use(
  expressSession({
    secret: process.env.SECRET,
    key: process.env.KEY,
    resave: true,
    saveUninitialized: true,
    cookie: {
      secure: true,
      httpOnly: true,
      sameSite: true,
    },
  })
);

passport.serializeUser(function (user, done) {
  done(null, user);
});
passport.deserializeUser(function (obj, done) {
  done(null, obj);
});

passport.use(
  new auth(
    {
      clientID: process.env.CLIENTID,
      clientSecret: process.env.CLIENTSECRET,
      callbackURL: process.env.CALLBACKURL,
      scope: process.env.SCOPE,
      profileFields: process.env.PROFILEFIELDS,
    },
    async function verify(accessToken, refreshToken, params, profile, done) {
      process.nextTick(function () {
        done(null, profile);
      });
    }
  )
);

app.get("/", auth, function (req, res) {
  res.json({
    user: {
      id: req.user.id,
      fullname: req.user.displayName,
      pname: req.user.name,
      sex: req.user.gender,
      url: req.user.url,
    },
  });
  /*
    RESULT:
    user.id: 449532928
    user.fullname: Mihail Bezmolenko
    user.pname: { "givenName": "Mihail", "familyName": "Bezmolenko" }
    user.sex: Мужской
    user.url: "http://vk.com/zeuvs"
    */
});

app.get("/logout", function (req, res) {
  req.logout();
  res.redirect("/");
});

app.get("/auth/vk", passport.authenticate("vkontakte"), function (req, res) {
  req.session.returnTo = req.originalUrl;
});

app.get(
  "/auth/vk/callback",
  passport.authenticate("vkontakte", {
    failureRedirect: "/",
    session: true,
  }),
  async function (req, res) {
    res.redirect(req.session.returnTo || "/");
    delete req.session.returnTo;
  }
);

async function auth(req, res, next) {
  if (!req.user) {
    req.session.returnTo = req.originalUrl;
    return res.redirect("/auth/vk/");
  }
  return next();
}

Package Sidebar

Install

npm i auth-vk

Weekly Downloads

3

Version

1.1.8

License

MIT

Unpacked Size

12.4 kB

Total Files

8

Last publish

Collaborators

  • zeuvs