Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.69 KB | None | 0 0
  1. module ApplicationCable
  2.   class Connection < ActionCable::Connection::Base
  3.     identified_by :current_user
  4.  
  5.     def connect
  6.       self.current_user = find_verified_user
  7.     end
  8.  
  9.     private
  10.     def find_verified_user
  11.       begin
  12.         header_array = request.headers[:HTTP_SEC_WEBSOCKET_PROTOCOL].split(',')
  13.         token = header_array[header_array.length-1].strip
  14.         decoded_token = JWT.decode token, nil, false
  15.  
  16.         if (current_user = ::Cliente.find(decoded_token[0]['cliente_id'].to_i))
  17.           current_user
  18.         else
  19.           reject_unauthorized_connection
  20.         end
  21.       rescue
  22.          reject_unauthorized_connection
  23.       end
  24.     end
  25.  
  26.    
  27.   end
  28. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement