Skip to content

Instantly share code, notes, and snippets.

@marshallswain
Created August 16, 2017 17:29
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 marshallswain/4f8faca09da3b2f98f8d435172771b3c to your computer and use it in GitHub Desktop.
Save marshallswain/4f8faca09da3b2f98f8d435172771b3c to your computer and use it in GitHub Desktop.
feathers-authentication Authenticate a socket client at connection time
const feathers = require('feathers/client')
const io = require('socket.io-client')
const axios = require('axios')
const rest = require('feathers-rest/client')
const socketio = require('feathers-socketio/client')
const auth = require('feathers-authentication-client')
const hooks = require('feathers-hooks')
// TODO: make this port dynamic like the server tests
const host = 'http://localhost:3030'
function makeClient (transport = 'socketio') {
const feathersClient = feathers()
if (transport === 'socketio') {
const socketOptions = {transports: ['websocket']}
const token = window.localStorage.getItem('feathers-jwt')
if (token) {
Object.assign(socketOptions, {
extraHeaders: {
Authorization: token
}
})
}
var socket = io(host, {
transports: ['websocket'],
extraHeaders: {
Authorization: 'jwt'
}
})
feathersClient.configure(socketio(socket, { timeout: 60000 }))
}
if (transport === 'rest') {
feathersClient.configure(rest(host).axios(axios))
}
feathersClient.configure(hooks())
.configure(auth())
return feathersClient
}
module.exports = [
makeClient('socketio'),
makeClient('rest')
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment