class Password::RecoveryService < ApplicationService
  def initialize(token)
    @token = token
  end

  def call
    return unless @token

    begin
      json = JWT.decode(@token, Rails.application.secrets.secret_key_base).first
    rescue
      return false
    end

    get_user(JSON.parse(json))
  end

  private
    def get_user(json)
      uid = json['uid']
      recovery_time = json['pw_recovery_time']

      User.find_by(id: uid, pw_recovery_time: recovery_time)
    end
end
