# auth-vk

> Authorization for websites via Vkontakte

Latest version **1.1.8** (published 2021-03-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install auth-vk
pnpm add auth-vk
yarn add auth-vk
bun add auth-vk
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.8 |
| Published | 2021-03-07 |
| First published | 2020-11-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=12.0.0 |
| Dependencies | 10 |
| Unpacked size | 12.2 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Zeu4s |
| Maintainers | zeuvs |
| Keywords | vk, api, sdk, bot, node, js, es6, es7, express, auth, vkauth, authvk, passport |

## Links

- npm: https://www.npmjs.com/package/auth-vk
- Repository: https://github.com/zeu45/auth-vk
- Homepage: https://github.com/zeu45/auth-vk#readme
- Issues: https://github.com/zeu45/auth-vk/issues
- npm.io page: https://npm.io/package/auth-vk

## Dependencies (10)

- [fs](https://npm.io/package/fs.md) 0.0.1-security
- [http](https://npm.io/package/http.md) 0.0.1-security
- [test](https://npm.io/package/test.md) ^0.6.0
- [util](https://npm.io/package/util.md) ^0.12.3
- [debug](https://npm.io/package/debug.md) ^4.3.1
- [https](https://npm.io/package/https.md) ^1.0.0
- [vk-io](https://npm.io/package/vk-io.md) ^4.0.4
- [express](https://npm.io/package/express.md) ^4.17.1
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.1
- [passport-oauth2](https://npm.io/package/passport-oauth2.md) ^1.5.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.8 (latest) — 2021-03-07
- 1.1.7 — 2021-03-07
- 1.1.5 — 2021-02-10
- 1.1.4 — 2021-02-10
- 1.1.3 — 2021-01-03
- 1.1.2 — 2020-11-21
- 1.1.1 — 2020-11-19

## README

<p align="center"><img src="https://s3.tproger.ru/uploads/2016/12/vk-java-auth.png"></p>
<p align="center">
<a href="https://www.npmjs.com/package/auth-vk"><img src="https://img.shields.io/npm/v/auth-vk.svg?style=flat-square" alt="NPM version"></a>
<a href="https://www.npmjs.com/package/auth-vk"><img src="https://img.shields.io/npm/dt/auth-vk.svg?style=flat-square" alt="NPM downloads"></a>
</p>

AUTH-VK is a powerful [Node.js](https://nodejs.org) a module that allows you to easily log in to Vkontakte 🚀

| 📖 [Documentation](https://www.npmjs.com/package/auth-vk) | 🤖 [Author](https://vk.com/zeuvs) |
| --------------------------------------------------------- | --------------------------------- |

## Features

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

## Installation

> **[Node.js](https://nodejs.org/) 12.0.0 or newer is required**

### NPM

```console
npm i auth-vk --save
npm i passport --save
```

### Interaction with the library

- After installing the library, you will need the following code:

```js
// 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:

<p align="center"><img src="https://dev-up.ru/img/start.png"></p>

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

<p align="center"><img src="https://dev-up.ru/img/next.png"></p>

```
ID приложения
Защищённый ключ
```

```js
clientID: 7624701,
clientSecret: 'xZUHQ8vgnMk4okBAKn1e',
```

- Next, you need to specify a link to your site for this example:

```js
callbackURL: "https://dev-up.ru/auth/vk/callback",
```

Then you need to specify the application rights and display the rights:<br>
You can get a list of rights here 📖 [Application Rights](https://vk.com/dev/permissions).<br>
In the example, we will specify the rights to: Access at any time, Groups, Email, Friends List.

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

That's it!

## Example usage

Sample code:

```js
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();
}
```

---
_Source: https://npm.io/package/auth-vk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
