UNPKG

remix-auth-vk

Version:

The VK strategy is used to authenticate users against a VK account. It extends the OAuth2Strategy.

72 lines (71 loc) 2.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VKStrategy = void 0; const remix_auth_oauth2_1 = require("remix-auth-oauth2"); class VKStrategy extends remix_auth_oauth2_1.OAuth2Strategy { constructor({ clientID, clientSecret, callbackURL, apiVersion, display, scope, userFields, }, verify) { super({ clientID, clientSecret, callbackURL, authorizationURL: 'https://oauth.vk.com/authorize', tokenURL: 'https://oauth.vk.com/access_token', }, verify); this.name = 'vk'; this.userInfoURL = 'https://api.vk.com/method/users.get'; this.scope = scope !== null && scope !== void 0 ? scope : 'email'; this.display = display !== null && display !== void 0 ? display : 'page'; this.userFields = userFields !== null && userFields !== void 0 ? userFields : [ 'has_photo', 'photo_max_orig', 'screen_name', ]; this.apiVersion = apiVersion !== null && apiVersion !== void 0 ? apiVersion : '5.131'; } authorizationParams() { return new URLSearchParams({ scope: this.scope, display: this.display, }); } async userProfile(accessToken, extraParams) { const url = new URL(this.userInfoURL); url.searchParams.append('access_token', accessToken); url.searchParams.append('user_ids', String(extraParams.user_id)); url.searchParams.append('v', this.apiVersion); url.searchParams.append('fields', this.userFields.join(',')); const response = await fetch(url.toString()); const contentType = response.headers.get('Content-Type'); const json = (contentType === null || contentType === void 0 ? void 0 : contentType.includes('application/json')) && (await response.json()); if (!response.ok) { return { provider: this.name, id: String(extraParams.user_id), displayName: '', name: { familyName: '', givenName: '', }, emails: [{ value: extraParams.email }], photos: [], _json: json !== null && json !== void 0 ? json : { response: [], }, }; } const [user] = json.response; return { provider: this.name, id: String(user.id), displayName: user.screen_name, name: { familyName: user.last_name, givenName: user.first_name, }, emails: [{ value: extraParams.email }], photos: [{ value: user.photo_max_orig }], _json: json, }; } } exports.VKStrategy = VKStrategy;