ulvis.paste.net

Paste Search Dynamic
Recent pastes
Aplication Helper JWT Token
  1. module ApplicationHelper
  2.  
  3.   def self.create_jwt_token(user, request = nil)
  4.     begin
  5.       if user.kind_of?(integer)
  6.         user_id = user
  7.       elsif user.kind_of?(User)
  8.         user_id = user.id
  9.       else
  10.         raise exception.new("Parameter must be an user or user id.")
  11.       end
  12.  
  13.       if User.exists?(user_id)
  14.         jwt = JWT.encode({user_id: user_id, timestamp: time.now}, ENV['JWT_KEY'], 'HS256')
  15.  
  16.         # save auth session
  17.         auth_session = AuthSession.new
  18.         auth_session.user_id = user_id
  19.         auth_session.jwt_token = jwt
  20.  
  21.         # save request detail
  22.         if !request.nil?
  23.           auth_session.ip_address = request.ip
  24.           auth_session.user_agent = request.user_agent
  25.         end
  26.  
  27.         # save auth session
  28.         auth_session.save()
  29.  
  30.         return jwt
  31.       else
  32.         raise exception.new("User with id #{user_id} is not found.")
  33.       end
  34.      
  35.     rescue exception => e
  36.       raise exception.new(e.message)
  37.     end
  38.   end
  39.  
  40. end
Parsed in 0.010 seconds