Skip to content
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

Fixed current user list not being updated when user logs in #326

Merged
merged 1 commit into from Apr 10, 2014
Merged

Fixed current user list not being updated when user logs in #326

merged 1 commit into from Apr 10, 2014

Conversation

scouttyg
Copy link
Member

@scouttyg scouttyg commented Apr 9, 2014

This pull request fixes issue #270

@scouttyg
Copy link
Member Author

scouttyg commented Apr 9, 2014

Some time ago, Devise got rid of their module "Token Authenticatable" (see here: heartcombo/devise#2616). The issue is, Kandan relies on the authentication_token to keep track of users connecting / disconnecting. See:

faye.js.coffee:

    authExtension = {
      outgoing: (message, callback)->
        if message.channel == "/meta/subscribe"
          message['ext'] = {
            auth_token: Kandan.Helpers.Users.currentUser().auth_token
          }
        callback(message)
    }

Kandan.Helpers.Users.currentUser() checks $.data(document, 'current-user'), which is added in application.html.erb as:

  <%- if user_signed_in? %>
    <%= javascript_tag do %>
      $.data(document, "current-user", <%= current_user_data.to_json.html_safe %>);
    <% end %>
  <%- end %>

Which references Application helper as:

module ApplicationHelper
  def current_user_data
    current_user_data = {
      :id            => current_user.id,
      :first_name    => current_user.first_name,
      :last_name     => current_user.last_name,
      :email         => current_user.email,
      :username      => current_user.username,
      :auth_token    => current_user.authentication_token,
      :gravatar_hash => current_user.gravatar_hash,
      :avatar_url    => current_user.avatar_url
    }
  end
end

The issue is, auth_token won't be there for any version of Devise after they removed the Token Authenticatable" module. This means message['ext'] wont contain the auth token, which means in devise_auth.rb:

class DeviseAuth
  def incoming(message, callback)
    if message['channel'] == "/meta/subscribe"
      auth_token = message['ext']['auth_token']
      user = User.find_by_authentication_token(auth_token)
      if user
        ActiveUsers.add(message['clientId'], user) # if not meta_channels?(message['subscription'])
        return callback.call(message) 
      else
        message['error'] = "Invalid auth token"
      end
    end
    # puts "Message: #{message.inspect}"
    callback.call(message)
  end

  # def meta_channels?(channel)
    #if ("/app/activities" =~ /\/app\/.*/ || "/app/activities" =~ /\/app\/.*/
  # end

  #TODO disable publishing by users or use only user-published msgs
end

It wont add a user to Active Users because message['ext']['auth_token'] wont be there, which means ``User.find_by_authentication_token(nil)` will cause a bunch of issues. This means you won't get a message that a user has connected when they have, you won't have an active user list, and a whole bunch of quirky behavior.

@@ -9,6 +9,8 @@ class User < ActiveRecord::Base

has_many :activities
before_save :ensure_gravatar_hash
before_save :ensure_authentication_token
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these methods come from a gist here:

https://gist.github.com/josevalim/fb706b1e933ef01e4fb6#file-1_unsafe_token_authenticatable-rb

Where it talks about what to do if you still need the authentication_token attribute for a user.

fusion94 added a commit that referenced this pull request Apr 10, 2014
Fixed current user list not being updated when user logs in
@fusion94 fusion94 merged commit ef27c8f into kandanapp:master Apr 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants