Skip to content

Instantly share code, notes, and snippets.

@claustres
Last active June 21, 2022 09:14
Show Gist options
  • Save claustres/2979cf3b054027796ebad4e127e76a93 to your computer and use it in GitHub Desktop.
Save claustres/2979cf3b054027796ebad4e127e76a93 to your computer and use it in GitHub Desktop.
OAuth strategy for Feathers v4
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