UNPKG

remix-auth-socials

Version:
75 lines 2.44 kB
import { OAuth2Strategy } from 'remix-auth-oauth2'; export const baseProfileFields = [ 'id', 'email', 'name', 'first_name', 'middle_name', 'last_name', 'picture', ]; export const FacebookStrategyName = 'facebook'; export const FacebookStrategyDefaultScopes = [ 'public_profile', 'email', ]; export const FacebookStrategyScopeSeperator = ','; export class FacebookStrategy extends OAuth2Strategy { name = FacebookStrategyName; scope; userInfoURL = 'https://graph.facebook.com/me'; profileFields; constructor({ clientId, clientSecret, redirectURI, scopes, extraProfileFields, }, verify) { super({ clientId, clientSecret, redirectURI, authorizationEndpoint: `https://facebook.com/v14.0/dialog/oauth`, tokenEndpoint: `https://graph.facebook.com/v14.0/oauth/access_token`, }, verify); this.scope = this.getScope(scopes); // Ensure unique entries in case they include the base fields this.profileFields = [ ...new Set([...baseProfileFields, ...(extraProfileFields || [])]), ]; } // Allow users the option to pass a scope string, or typed array getScope(scopes) { if (!scopes) { return FacebookStrategyDefaultScopes; } else if (typeof scopes === 'string') { return scopes.split(FacebookStrategyScopeSeperator); } return scopes; } authorizationParams() { const params = new URLSearchParams({ scope: this.scope.join(FacebookStrategyScopeSeperator), }); return params; } async userProfile(accessToken) { const requestParams = `?fields=${this.profileFields.join(',')}`; const requestUrl = `${this.userInfoURL}${requestParams}`; const response = await fetch(requestUrl, { headers: { Authorization: `Bearer ${accessToken}`, }, }); const raw = await response.json(); const profile = { id: raw.id, displayName: raw.name, name: { familyName: raw.last_name, givenName: raw.first_name, }, emails: [{ value: raw.email }], photos: [{ value: raw.picture.data.url }], _json: raw, }; return profile; } } //# sourceMappingURL=facebook.js.map