feathers-authentication-local
Version:
Local authentication strategy for feathers-authentication
63 lines (53 loc) • 1.68 kB
JavaScript
var feathers = require('feathers');
var rest = require('feathers-rest');
var socketio = require('feathers-socketio');
var hooks = require('feathers-hooks');
var memory = require('feathers-memory');
var bodyParser = require('body-parser');
var errorHandler = require('feathers-errors/handler');
var auth = require('feathers-authentication');
var jwt = require('feathers-authentication-jwt');
var local = require('../lib/index');
// Initialize the application
var app = feathers();
app.configure(rest())
.configure(socketio())
.configure(hooks())
// Needed for parsing bodies (login)
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
// Configure feathers-authentication
.configure(auth({ secret: 'super secret' }))
.configure(local())
.configure(jwt())
.use('/users', memory())
.use(errorHandler());
// Authenticate the user using the default
// email/password strategy and if successful
// return a JWT.
app.service('authentication').hooks({
before: {
create: [
auth.hooks.authenticate('local')
]
}
});
// Add a hook to the user service that automatically replaces
// the password with a hash of the password before saving it.
app.service('users').hooks({
before: {
get: auth.hooks.authenticate('jwt'),
create: local.hooks.hashPassword()
}
});
// Create a user that we can use to log in
var User = {
email: 'admin@feathersjs.com',
password: 'admin',
permissions: ['*']
};
app.service('users').create(User).then(user => {
console.log('Created default user', user);
}).catch(console.error);
app.listen(3030);
console.log('Feathers authentication with local auth started on 127.0.0.1:3030');