Created
August 16, 2017 17:29
-
-
Save marshallswain/4f8faca09da3b2f98f8d435172771b3c to your computer and use it in GitHub Desktop.
feathers-authentication Authenticate a socket client at connection time
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
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