class Api::V1::AuthenticationController
Public Instance Methods
authenticate_user()
click to toggle source
# File lib/generators/jwt_api/templates/api/v1/authentication_controller.rb, line 6 def authenticate_user user = User.find_for_database_authentication(email: params[:email]) if !user.nil? && user.valid_password?(params[:password]) render json: payload(user) else render json: { errors: ['Invalid Username/Password'] }, status: :unauthorized end end
logout()
click to toggle source
Invalidate users JWT, logout user
# File lib/generators/jwt_api/templates/api/v1/authentication_controller.rb, line 16 def logout @current_user.jti = SecureRandom.uuid if @current_user.save render json: { success: true } else render json: { success: false }, status: :unprocessable_entity end end
Private Instance Methods
payload(user)
click to toggle source
# File lib/generators/jwt_api/templates/api/v1/authentication_controller.rb, line 27 def payload(user) return nil unless user&.id iat = Time.now.to_i exp = Time.now.to_i + 24 * 3600 { token: JsonWebToken.encode({ user_id: user.id, jti: user.jti, iat: iat, exp: exp }) } end