Last active
June 21, 2022 09:14
-
-
Save claustres/2979cf3b054027796ebad4e127e76a93 to your computer and use it in GitHub Desktop.
OAuth strategy for Feathers v4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash' | |
import { OAuthStrategy } from '@feathersjs/authentication-oauth' | |
export class AuthenticationProviderStrategy extends OAuthStrategy { | |
async getEntityData (profile, entity) { | |
const createEntity = _.isNil(entity) | |
// Add provider Id | |
entity = { [`${this.name}Id`]: profile.id || profile.sub } | |
// When creating a new user extract required information from profile | |
if (createEntity) { | |
_.set(entity, 'email', _.get(profile, this.emailFieldInProfile || 'email')) | |
_.set(entity, 'name', _.get(profile, this.nameFieldInProfile || 'name')) | |
} | |
// Store provider profile information | |
entity[`${this.name}`] = profile | |
return entity | |
} | |
async getEntityQuery (profile) { | |
return query = { | |
$or: [ | |
{ [`${this.name}Id`]: profile.id || profile.sub }, | |
{ email: _.get(profile, this.emailFieldInProfile || 'email' ) } | |
], | |
$limit: 1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment