Created
February 5, 2017 08:36
-
-
Save shirish87/a61a872ae322541a872c1e7ebd05f675 to your computer and use it in GitHub Desktop.
Authentication using external token microservice. file: src/services/authentication/index.js
This file contains 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
'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