diff --git a/src/providers/index.js b/src/providers/index.js index 389a65f246..2076bbe833 100644 --- a/src/providers/index.js +++ b/src/providers/index.js @@ -6,8 +6,8 @@ import Basecamp from './basecamp' import BattleNet from './battlenet' import Box from './box' import Bungie from './bungie' -import Credentials from './credentials' import Cognito from './cognito' +import Credentials from './credentials' import Discord from './discord' import Email from './email' import Facebook from './facebook' @@ -27,19 +27,20 @@ import Spotify from './spotify' import Strava from './strava' import Twitch from './twitch' import Twitter from './twitter' +import VK from './vk' import Yandex from './yandex' export default { + Apple, Atlassian, Auth0, - Apple, AzureADB2C, Basecamp, BattleNet, Box, Bungie, - Credentials, Cognito, + Credentials, Discord, Email, Facebook, @@ -57,7 +58,8 @@ export default { Slack, Spotify, Strava, - Twitter, Twitch, + Twitter, + VK, Yandex } diff --git a/src/providers/vk.js b/src/providers/vk.js new file mode 100644 index 0000000000..2aae937b6b --- /dev/null +++ b/src/providers/vk.js @@ -0,0 +1,30 @@ +export default (options) => { + const apiVersion = '5.126' // https://vk.com/dev/versions + + return { + id: 'vk', + name: 'vk.com', + type: 'oauth', + version: '2.0', + scope: 'email', + params: { + grant_type: 'authorization_code' + }, + accessTokenUrl: `https://oauth.vk.com/access_token?v=${apiVersion}`, + requestTokenUrl: `https://oauth.vk.com/access_token?v=${apiVersion}`, + authorizationUrl: + `https://oauth.vk.com/authorize?response_type=code&v=${apiVersion}`, + profileUrl: `https://api.vk.com/method/users.get?fields=photo_100&v=${apiVersion}`, + profile: (result) => { + const profile = result.response?.[0] ?? {} + + return { + id: profile.id, + name: [profile.first_name, profile.last_name].filter(Boolean).join(' '), + email: profile.email, + image: profile.photo_100 + } + }, + ...options + } +} diff --git a/src/server/lib/oauth/client.js b/src/server/lib/oauth/client.js index 5ca66a95e8..ba7de95640 100644 --- a/src/server/lib/oauth/client.js +++ b/src/server/lib/oauth/client.js @@ -184,8 +184,8 @@ async function getOAuth2 (provider, accessToken, results) { if (this._useAuthorizationHeaderForGET) { headers.Authorization = this.buildAuthHeader(accessToken) - // Mail.ru requires 'access_token' as URL request parameter - if (provider.id === 'mailru') { + // Mail.ru & vk.com require 'access_token' as URL request parameter + if (['mailru', 'vk'].includes(provider.id)) { const safeAccessTokenURL = new URL(url) safeAccessTokenURL.searchParams.append('access_token', accessToken) url = safeAccessTokenURL.href diff --git a/www/docs/configuration/providers.md b/www/docs/configuration/providers.md index 2aaa8d5c29..fc912f0a42 100644 --- a/www/docs/configuration/providers.md +++ b/www/docs/configuration/providers.md @@ -38,6 +38,7 @@ NextAuth.js is designed to work with any OAuth service, it supports OAuth 1.0, 1 * [Strava](/providers/strava) * [Twitch](/providers/Twitch) * [Twitter](/providers/twitter) +* [VK](/providers/vk) * [Yandex](/providers/yandex) ### Using a built-in OAuth provider diff --git a/www/docs/providers/vk.md b/www/docs/providers/vk.md new file mode 100644 index 0000000000..7a3488c284 --- /dev/null +++ b/www/docs/providers/vk.md @@ -0,0 +1,49 @@ +--- +id: vk +title: vk.com +--- + +## Documentation + +https://vk.com/dev/first_guide + +## Configuration + +https://vk.com/apps?act=manage + +## Example + +```js +import Providers from `next-auth/providers` +... +providers: [ + Providers.VK({ + clientId: process.env.VK_CLIENT_ID, + clientSecret: process.env.VK_CLIENT_SECRET + }) +] +... +``` + +:::note +By default the provider uses `5.126` version of the API. See https://vk.com/dev/versions for more info. +::: + +If you want to use a different version, you can pass it to provider's options object: + +```js +// pages/api/auth/[...nextauth].js + +const apiVersion = "5.126" +... +providers: [ + Providers.VK({ + accessTokenUrl: `https://oauth.vk.com/access_token?v=${apiVersion}`, + requestTokenUrl: `https://oauth.vk.com/access_token?v=${apiVersion}`, + authorizationUrl: + `https://oauth.vk.com/authorize?response_type=code&v=${apiVersion}`, + profileUrl: `https://api.vk.com/method/users.get?fields=photo_100&v=${apiVersion}`, + }) +] +... +``` diff --git a/www/sidebars.js b/www/sidebars.js index 39864204fa..a2f9abbb64 100644 --- a/www/sidebars.js +++ b/www/sidebars.js @@ -53,6 +53,7 @@ module.exports = { 'providers/strava', 'providers/twitch', 'providers/twitter', + 'providers/vk', 'providers/yandex' ] }