Skip to content

Instantly share code, notes, and snippets.

@Tinitto
Created August 8, 2019 01:09
Show Gist options
  • Save Tinitto/b9cc407265e1a8e65f44c5b3e000ed78 to your computer and use it in GitHub Desktop.
Save Tinitto/b9cc407265e1a8e65f44c5b3e000ed78 to your computer and use it in GitHub Desktop.
Custom Google OauthStrategy for Feathersjs 4 with email as primary key
const {
AuthenticationService,
JWTStrategy
} = require('@feathersjs/authentication');
const {
expressOauth,
OAuthStrategy
} = require('@feathersjs/authentication-oauth');
class AdvancedGoogleStrategy extends OAuthStrategy {
async getEntityQuery(profile, _params) {
await super.getEntityQuery(profile, _params);
return {
email: profile.email
};
}
async getEntityData(profile, _existingEntity, _params) {
const baseData = await super.getEntityData(
profile,
_existingEntity,
_params
);
// Get more fields from profile e.g. name, email, picture, given_name, family_name
const { name, email } = profile;
const extendedProfile = Object.assign({}, baseData, {
name,
email,
});
return extendedProfile;
}
}
module.exports = app => {
const authentication = new AuthenticationService(app);
authentication.register('jwt', new JWTStrategy());
authentication.register('google', new AdvancedGoogleStrategy());
app.use('/authentication', authentication);
app.configure(
expressOauth({
linkStrategy: 'google'
})
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment