1.1.3 • Published 4 years ago

node-vkauth v1.1.3

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

NODE-VKAUTH 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

Yarn

Recommended

yarn add node-vkauth

NPM

npm i node-vkauth
npm i passport

Example usage

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

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

passport.use(
  new AuthVK(
    {
      clientID: 'ID приложения',
      clientSecret: "Секретный ключ",
      callbackURL: "https://example.com/auth/vk/callback",
      scope: [ 'bit masks' ],
      profileFields: ['bit masks'],
    },
    async function verify(accessToken, refreshToken, params, profile, done) {
      process.nextTick(function () {
        done(null, profile);
      });
    }
  )
);

app.get("/", autch, function (req, res) {
    res.redirect("ejs/index.ejs", {
        user: req.user
    });
  });

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 autch(req, res, next) {
    if (!req.user) {
      req.session.returnTo = req.originalUrl;
        return res.redirect('/auth/vk/');
      }
    }
    return next();
  }