Skip to content

Instantly share code, notes, and snippets.

@rithvikvibhu
Created Jan 16, 2017
Embed
What would you like to do?
Feathers socket auth - Server side
const path = require('path');
const serveStatic = require('feathers').static;
const favicon = require('serve-favicon');
const compress = require('compression');
const cors = require('cors');
const feathers = require('feathers');
const configuration = require('feathers-configuration');
const hooks = require('feathers-hooks');
const rest = require('feathers-rest');
const bodyParser = require('body-parser');
const auth = require('feathers-authentication');
const socketio = require('feathers-socketio');
const jwt = require('feathers-authentication-jwt');
const local = require('feathers-authentication-local');
const middleware = require('./middleware');
const services = require('./services');
const mongoose = require('mongoose');
const service = require('feathers-mongoose');
const foodModel = require('./models/foodModel');
const orderModel = require('./models/orderModel');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/feathers');
const app = feathers();
app.configure(configuration(path.join(__dirname, '..')));
let socketioOptions = function(io) {
io.on('connection', function(socket) {
socket.emit('test', { text: 'A client connected! This message is sent right after connecting.' });
socket.on('test', function (data) {
console.log(data);
});
});
// Registering Socket.io middleware
io.use(function (socket, next) {
// Exposing a request property to services and hooks
socket.feathers.referrer = socket.request.referrer;
next();
});
};
app.use(compress())
.options('*', cors())
.use(cors())
.use(favicon( path.join(app.get('public'), 'favicon.ico') ))
.use('/', serveStatic( app.get('public') ))
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(hooks())
.configure(rest())
.configure(socketio())
.configure(services)
.configure(middleware)
.configure(jwt())
.configure(local());
app.service('authentication').hooks({
before: {
create: [
auth.hooks.authenticate(['jwt', 'local'])
]
}
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment