Skip to content

Commit

Permalink
Ensure users who log out no longer receive events
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen Barnes committed Jul 26, 2012
1 parent ce69fc2 commit 86444b0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -23,6 +23,7 @@ Not yet released. These are the changes so far...
* Added documentation in Korean (thanks EngForDev)
* Enable proper handling of question marks and params when routing HTTP requests (thanks matthiasg)
* In newly generated projects `ss.define.client()` now lists client libs explicitly to avoid confusion over load order
* Added ability to call `req.session.setUserId(null, cb)` when a user signs out
* Updated bundled jQuery to 1.7.2


Expand Down
6 changes: 5 additions & 1 deletion doc/guide/en/authentication.md
Expand Up @@ -30,7 +30,11 @@ exports.actions = function(req, res, ss){
res('Access denied!');
}

}
},

logout: function(){
req.session.setUserId(null);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions doc/guide/en/pub_sub_events.md
Expand Up @@ -70,6 +70,8 @@ Once a user has been [authenticated](https://github.com/socketstream/socketstrea
ss.publish.user('fred', 'specialOffer', 'Here is a special offer just for you!');
```

Important: When a user signs out of your app, you should call `req.session.setUserId(null, cb)` to prevent the browser from receiving future events addressed to that `userId`. Note: This command only affects the current session. If the user is logged in via other devices/sessions these will be unaffected.



### 4. Sending to Individual Clients (browser tabs)
Expand Down
9 changes: 7 additions & 2 deletions lib/session/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/session/index.coffee
Expand Up @@ -51,8 +51,12 @@ exports.find = (sessionId, socketId, cb) ->
session.channel = channels(session, socketId)

session.setUserId = (userId, cb = ->) ->
@userId = userId
@_bindToSocket()
if userId
@userId = userId
@_bindToSocket()
else if @userId # if null (i.e. user has signed out)
subscriptions.user.remove(@userId, socketId)
delete @userId
@save(cb)

session._bindToSocket = ->
Expand Down

0 comments on commit 86444b0

Please sign in to comment.