-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/buyer approve request #53
Changes from 1 commit
7dc23f1
e1edbd3
06199e3
9c45fe1
6ea2871
c13e56e
0772054
355acb3
a3e53de
1544fc8
b7e852b
0b5df8f
9d9404e
96340cd
5c98f79
acb7f89
0f97a38
324424f
f39c963
2b06b52
c9b08a1
4f63cec
c9e456b
9e3d8a3
935086d
9f82555
172e2cd
c5a0f79
484caa1
d194d40
40d106c
7b305a9
3a5d19b
6c33ecb
2be5c18
3cef28c
f8f4dca
db20090
fa39e36
9743c94
a64406d
abcbf8b
a2b4040
725dfdc
dcd1e05
3e576f8
15987a6
1271098
af84911
8a6ffd8
ddaba4a
5a40ca5
8d9c626
5c5d71f
7d274bd
db6aa20
a1932c2
2d42548
cef9ab7
67e5c51
336200c
d9e6ac3
85e44da
03bd39e
3a9d73d
d0108c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { getAccount, logout } from 'actions/authActions'; | ||
import notifyHub from 'socket/notify'; | ||
|
||
/* istanbul ignore next */ | ||
export const requireAuth = (store) => (nextState, replace, done) => { | ||
|
@@ -11,6 +12,11 @@ export const requireAuth = (store) => (nextState, replace, done) => { | |
pathname: '/login', | ||
state: { nextPathname: nextState.location.pathname } | ||
}); | ||
} else { | ||
const socket = notifyHub.getInstance(); | ||
if (socket.disconnected) { | ||
socket.connect(); | ||
} | ||
} | ||
|
||
done(); | ||
|
@@ -30,6 +36,10 @@ export const requireAuth = (store) => (nextState, replace, done) => { | |
/* istanbul ignore next */ | ||
export const handleLogout = (store) => (nextState, replace) => { | ||
store.dispatch(logout()); | ||
const socket = notifyHub.getInstance(); | ||
if (socket.connected) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the note above |
||
socket.disconnect(); | ||
} | ||
replace({ | ||
pathname: '/login' | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* eslint no-console: 0 */ | ||
import { SOCKET_URL } from 'constants/index'; | ||
import io from 'socket.io-client'; | ||
|
||
const link = `${SOCKET_URL}notify`; | ||
|
||
const notifyHub = (function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that this is client-side where they'll only have on socket on the Alternatively you could consider a class to wrap the socket. |
||
let socket; | ||
|
||
function init() { | ||
socket = io(link); | ||
|
||
socket.on('connect', function () { | ||
console.log('connected', socket); | ||
}); | ||
|
||
socket.on('child_added', function (data) { | ||
console.log(data); | ||
}); | ||
|
||
socket.on('connection_refused', function (err) { | ||
console.log(err); | ||
}); | ||
|
||
socket.on('disconnect', function () { | ||
}); | ||
|
||
return socket; | ||
} | ||
|
||
return { | ||
getInstance: function () { | ||
|
||
if ( !socket ) { | ||
socket = init(); | ||
} | ||
|
||
return socket; | ||
} | ||
|
||
}; | ||
|
||
})(); | ||
|
||
export default notifyHub; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var firebase = require('firebase'); | ||
var config = require('config.js'); | ||
|
||
firebase.initializeApp({ | ||
databaseURL: config.get('FIREBASE_URL'), | ||
serviceAccount: config.get('FIREBASE_KEY_PATH') | ||
}); | ||
|
||
module.exports = firebase.database(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var authorize = require('socket-server/cookie-authorize'); | ||
var db = require('firebase-db'); | ||
var ref = db.ref('notification'); | ||
|
||
var socketMap = {}; | ||
|
||
var subscribe = function (socket) { | ||
authorize(socket).then(function (username) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could break this out using the socketio-jwt middleware as discussed on gitter |
||
socketMap[username] = socket; | ||
}).catch(function (err) { | ||
socket.emit('connection_refused', err); | ||
}); | ||
}; | ||
|
||
ref.on('child_added', function (snapshot, prevChildKey) { | ||
var notification = snapshot.val(); | ||
var socket = notification.to && socketMap[notification.to]; | ||
if (socket) { | ||
socket.emit('child_added', notification); | ||
} | ||
}); | ||
|
||
module.exports = { | ||
subscribe: subscribe | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var cookie = require('cookie'); | ||
var jwt = require('jsonwebtoken'); | ||
|
||
var config = require('config.js'); | ||
|
||
module.exports = function authorize(socket) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, if you use socketio-jwt or some other alternative you can put the auth stuff in |
||
return new Promise(function (resolve, reject) { | ||
var cookies = cookie.parse(socket.request.headers.cookie); | ||
if (cookies.token) { | ||
jwt.verify(cookies.token.replace('JWT ', ''), config.get('API_SECRET'), function (err, decoded) { | ||
if (!err) { | ||
var username = decoded && decoded.username; | ||
if (username) { | ||
resolve(username); | ||
} else { | ||
reject('Invalid token'); | ||
} | ||
} else { | ||
reject(err); | ||
} | ||
}); | ||
} else { | ||
reject('No token provided'); | ||
} | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var notificationHub = require('notification/hub'); | ||
|
||
module.exports = function (httpServer) { | ||
var io = require('socket.io')(httpServer); | ||
|
||
io.of('/notify').on('connection', function (socket) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I normally keep a reference to the namespace when creating one as the namespace object has plenty of useful methods (especially server-side) when managing multiple clients. const nsp = io.of('/notify');
nsp.on('connection', function (socket) { |
||
notificationHub.subscribe(socket); | ||
|
||
socket.on('disconnect', function () { | ||
|
||
}); | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
connected
anddisconnected
props are not part of the public API of socket.io and have changed in the past, it's up to you whether you still want to use them but I'd suggest keeping track of the state outside of the socket object and changing it in the event handlers.