From 839250e26267a5d7438338665c2d7533dda10786 Mon Sep 17 00:00:00 2001 From: Zlatko Zahariev Date: Wed, 5 Jun 2019 14:09:34 +0300 Subject: [PATCH 1/3] Add some examples for getting current_user Add some examples for getting current_user in ActionCable::Connection. It is somehow common for Rails to use a session in order to keep current user user_id. The most common authentication approach is with Devise. This PR suggest some more info on how to obtain current_user with Devise and how to obtain current_user in a more general situation when we have a session cookie. Also in the text a "signed" cookie is mentioned but in the code above is used an "encrypted" cookie. --- guides/source/action_cable_overview.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/guides/source/action_cable_overview.md b/guides/source/action_cable_overview.md index f1e2a0081f..5a59e5a48d 100644 --- a/guides/source/action_cable_overview.md +++ b/guides/source/action_cable_overview.md @@ -111,7 +111,7 @@ specific connection later. Note that anything marked as an identifier will autom create a delegate by the same name on any channel instances created off the connection. This example relies on the fact that you will already have handled authentication of the user -somewhere else in your application, and that a successful authentication sets a signed +somewhere else in your application, and that a successful authentication sets an encrypted cookie with the user ID. The cookie is then automatically sent to the connection instance when a new connection @@ -120,6 +120,17 @@ by this same current user, you're also ensuring that you can later retrieve all connections by a given user (and potentially disconnect them all if the user is deleted or unauthorized). +If you use Device for authenticaion, you can get `current_user` from warden: + +```ruby + verified_user = env['warden'].user +``` + +In any other authentication approach you can access the session cookie. If you use cookie store for the session, your session cookie is named "\_session" and the user ID key is "user_id" you can use this approach: +```ruby + verified_user = User.find_by(id: cookies.encrypted['_session']['user_id']) +``` + ### Channels A *channel* encapsulates a logical unit of work, similar to what a controller does in a From 319b4968824b3dab16d851c61ef4c4ae297280ac Mon Sep 17 00:00:00 2001 From: Zlatko Zahariev Date: Fri, 7 Jun 2019 18:13:14 +0300 Subject: [PATCH 2/3] [ci skip] Fix a typo --- guides/source/action_cable_overview.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guides/source/action_cable_overview.md b/guides/source/action_cable_overview.md index 5a59e5a48d..622cb0b4ea 100644 --- a/guides/source/action_cable_overview.md +++ b/guides/source/action_cable_overview.md @@ -120,7 +120,7 @@ by this same current user, you're also ensuring that you can later retrieve all connections by a given user (and potentially disconnect them all if the user is deleted or unauthorized). -If you use Device for authenticaion, you can get `current_user` from warden: +If you use Devise for authenticaion, you can get `current_user` from warden: ```ruby verified_user = env['warden'].user From 290d82ada0aae05f2f1364e3a50a9b1e3f77245c Mon Sep 17 00:00:00 2001 From: Zlatko Zahariev Date: Mon, 24 Jun 2019 19:12:44 +0300 Subject: [PATCH 3/3] [ci skip] Remove mentioning of the devise gem --- guides/source/action_cable_overview.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/guides/source/action_cable_overview.md b/guides/source/action_cable_overview.md index 622cb0b4ea..0ba36cb2d2 100644 --- a/guides/source/action_cable_overview.md +++ b/guides/source/action_cable_overview.md @@ -120,13 +120,9 @@ by this same current user, you're also ensuring that you can later retrieve all connections by a given user (and potentially disconnect them all if the user is deleted or unauthorized). -If you use Devise for authenticaion, you can get `current_user` from warden: - -```ruby - verified_user = env['warden'].user -``` - -In any other authentication approach you can access the session cookie. If you use cookie store for the session, your session cookie is named "\_session" and the user ID key is "user_id" you can use this approach: +If your authentication approach includes using a session, you use cookie store for the +session, your session cookie is named `_session` and the user ID key is `user_id` you +can use this approach: ```ruby verified_user = User.find_by(id: cookies.encrypted['_session']['user_id']) ```