Skip to content

Instantly share code, notes, and snippets.

@nriesco
Last active June 10, 2020 05:58
Show Gist options
  • Save nriesco/27ae0128c4d03a1f72846898669d54c5 to your computer and use it in GitHub Desktop.
Save nriesco/27ae0128c4d03a1f72846898669d54c5 to your computer and use it in GitHub Desktop.
const { AuthenticationService, JWTStrategy } = require('@feathersjs/authentication');
const { LocalStrategy } = require('@feathersjs/authentication-local');
const { expressOauth, OAuthStrategy } = require('@feathersjs/authentication-oauth');
class GoogleStrategy extends OAuthStrategy {
// https://github.com/feathersjs/feathers/blob/be91206e3dba1e65a81412b7aa636bece3ab4aa2/packages/authentication-oauth/src/strategy.ts#L113-L119
async createEntity(profile, params) {
// check if the user exists and has access
if (youShouldLetHimIn) {
// access your services via this.app.service('service-name')
return this.app.service('var').create(someData);
} else {
// decide what to do
if (someDecision) {
return super.createEntity(profile, params);
} else {
return {}; // not sure if this should be an error or just an empty object
}
}
}
// https://github.com/feathersjs/feathers/blob/be91206e3dba1e65a81412b7aa636bece3ab4aa2/packages/authentication-oauth/src/strategy.ts#L121-L128
async updateEntity(entity, profile, params) {
// check if the user exists and has access
if (youShouldLetHimIn) {
// access your services via this.app.service('service-name')
return this.app.service('var').patch(someId, someData);
} else {
// decide what to do
if (someDecision) {
return super.createEntity(profile, params);
} else {
return {}; // not sure if this should be an error or just an empty object
}
}
}
async getEntityData(profile, entity, params) {
const baseData = await super.getEntityData(profile);
let { email, picture} = profile;
let data = {
...baseData,
profilePicture: picture,
email
};
return data;
}
}
module.exports = app => {
const authentication = new AuthenticationService(app);
authentication.register('jwt', new JWTStrategy());
authentication.register('local', new LocalStrategy());
authentication.register('google', new GoogleStrategy()); // added!
app.use('/authentication', authentication);
app.configure(expressOauth());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment