roysathome.net/auth/post.rb

60 lines
1.5 KiB
Ruby

require 'net/http'
require 'uri'
require 'json'
# The URL you want to send the POST request to
url = URI.parse('http://localhost:4567/auth/login')
# Create a new Net::HTTP object with the target server
http = Net::HTTP.new(url.host, url.port)
# If your server uses HTTPS, you might need to enable SSL
# http.use_ssl = true
# Create a new Net::HTTP::Post request with the desired path
request = Net::HTTP::Post.new(url.path)
puts 'Enter username:'
username = gets.strip!
puts 'Enter password'
password = gets.strip!
# Set the request body with the data you want to send
request.body = "username=#{username}&password=#{password}"
#request.body = "username=admin&password=pass123"
# Set the 'Content-Type' header if needed
request['Content-Type'] = 'application/x-www-form-urlencoded'
# Send the request and get the response
response = http.request(request)
unless response.code == '200'
puts "Response Code: #{response.code}. Aborting."
puts "Response Body: #{response.body}"
puts "Aborting."
exit
end
puts 'Reauthentication token stored.'
response_data = JSON.parse(response.body)
reauthentication_token = response_data['token']
url = URI.parse('http://localhost:4567/auth/reauthenticate')
request = Net::HTTP::Post.new(url.path)
request['Authorization'] = "Bearer #{reauthentication_token}"
response = http.request(request)
unless response.code == '200'
puts "Response Code: #{response.code}. Aborting."
puts "Response Body: #{response.body}"
puts "Aborting."
exit
end
puts response.body