Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/nsrr/helpers/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

require "nsrr/helpers/color"
require "nsrr/helpers/json_request"
require 'io/console'

module Nsrr
module Helpers
# Helper to verify that user is authenticated.
class Authorization
def self.get_token(token)
puts " Get your token here: " + "#{Nsrr::WEBSITE}/token".bg_gray.blue.underline
puts " Your input is hidden while entering token.".white
print " Enter your token: "
token = STDIN.noecho(&:gets).chomp if token.to_s.strip == ""
token.strip!
# Try to read the token from the environment.
token = ENV['NSRR_TOKEN']
# Check if the token is nil or empty
if token.to_s.strip.empty?
puts " Get your token here: " + "#{Nsrr::WEBSITE}/token".bg_gray.blue.underline
puts " Your input is hidden while entering token.".white
print " Enter your token: "
token = STDIN.noecho(&:gets).chomp if token.to_s.strip == ""
token.strip!
else
puts " Using token from environment variable NSRR_TOKEN."
end
(response, _status) = Nsrr::Helpers::JsonRequest.get("#{Nsrr::WEBSITE}/api/v1/account/profile.json", auth_token: token)
if response.is_a?(Hash) && response["authenticated"]
puts "AUTHORIZED".green + " as " + "#{response["first_name"]} #{response["last_name"]}".white
Expand Down