Skip to content

Instantly share code, notes, and snippets.

@shirish87
Created February 5, 2017 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirish87/a61a872ae322541a872c1e7ebd05f675 to your computer and use it in GitHub Desktop.
Save shirish87/a61a872ae322541a872c1e7ebd05f675 to your computer and use it in GitHub Desktop.
Authentication using external token microservice. file: src/services/authentication/index.js
'use strict';
const auth = require('feathers-authentication');
const request = require('request');
module.exports = function() {
const app = this;
const config = app.get('auth');
const authHost = config.local.authHost;
config.token.payload = ['token'];
config.token.jwt = {
verify(token, secret, options, callback) {
request.get(`${authHost}/api/v1/auth/tokens/verify`, {
auth: { bearer: token },
json: true
}, (err, r, data) => {
callback(err, data && { id: data.user_id, token: token });
});
},
sign(data, secret, options, callback) {
callback(data.token);
}
};
config.local.checkCredentials = (req, username, password, done) => {
const formData = Object.assign({}, config.local.service.params, { username, password });
request.post(`${authHost}/api/v1/auth/token`, { form: formData, json: true }, (err, r, token) => {
if (err || !token) {
return done(err || new Error('Failed to authenticate. Please try again.'));
}
let { access_token, error_description } = token;
if (error_description || !access_token) {
console.log('error', username, error_description);
return done(null, false);
}
done(null, { id: 'me', token: access_token });
});
};
app.configure(auth(config));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment