Skip to content

Instantly share code, notes, and snippets.

@PhilWhitehurst
Created April 6, 2016 08:19
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 PhilWhitehurst/ceba63b595b40fac04bbcecbeccf6946 to your computer and use it in GitHub Desktop.
Save PhilWhitehurst/ceba63b595b40fac04bbcecbeccf6946 to your computer and use it in GitHub Desktop.
Feathersjs app with authentication
var feathers = require('feathers');
var rest = require('feathers-rest');
var socketio = require('feathers-socketio');
var hooks = require('feathers-hooks');
var authentication = require('feathers-authentication');
var authHooks = authentication.hooks;
var bodyParser = require('body-parser');
var handler = require('feathers-errors/handler');
// Express compression
var compression = require("compression");
// Configure application
var app = feathers();
app.use(compression());
app.use(feathers.static('html', {
maxAge: maxAge
}));
// Parse HTTP JSON bodies
app.use(bodyParser.json());
// Parse URL-encoded params
app.use(bodyParser.urlencoded({extended: true}));
// Register hooks module
app.configure(hooks());
// Add REST API support
app.configure(rest());
// Configure Socket.io real-time APIs
app.configure(socketio());
// Register our authentication plugin
app.configure(authentication());
// Register our aws dynamodb "users" service
app.use('/users', db_users);
// Register a nicer error handler than the default Express one
app.use(handler());
......
// Register a before hook to hash passwords
app.service('users').before({
create: authentication.hooks.hashPassword()
});
//setup profile service
app.service('profile', {
find:
function (params) {
return Promise.resolve("hi");
}
});
// Register a before hook to get user
app.service('profile').before({
find: [
authentication.hooks.verifyToken(),
authentication.hooks.populateUser(),
function (hook) {
console.log(hook);
return hook;
}
]
});
var socket = io();
// setup feathers client side
var app = feathers();
// Register hooks module
app.configure(feathers.hooks());
// Register socket.io
app.configure(feathers.socketio(socket));
// Set up a store to cache your auth token and user
app.use('storage', localstorage({storage: localStorage}));
// Set up authentication
app.configure(feathers.authentication());
this.connected = function () {
console.log("Connection established");
// If we have a token try and authenticate with it
app.token()
.then(function (token) {
return app.authenticate({
type: 'token',
'token': token
})
.then(function (result) {
// Handles a user logging in with email and password
this.authenticate = function (evt, data) {
var _this = this;
app.authenticate({
type: 'local',
'email': data.email,
'password': data.password
}).then(function (result) {
console.log('Authenticated');
_this.trigger(document, "authSuccess");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment