Skip to content

Instantly share code, notes, and snippets.

@maaand
Created August 5, 2015 14:42
Show Gist options
  • Save maaand/1897e5f5adad9c6fcec0 to your computer and use it in GitHub Desktop.
Save maaand/1897e5f5adad9c6fcec0 to your computer and use it in GitHub Desktop.
# This is a temporary solution for login via the API call we have.
def login
begin
flash[:notice] = nil
email = params["email"]
password = params["password"]
puts email
puts password
#This gets the user's id number (which is the only piece of info we need from the user to login)
login_response = HTTParty.post("https://dev.kitchology.com/api/v1/users/login",
:body => {grant_type: "password", email: email, password: password})
logger.info login_response
if login_response.parsed_response["userName"]
logger.info "In"
session[:user_name] = login_response.parsed_response["userName"]
end
if login_response.parsed_response["access_token"]
logger.info "In"
session[:access_token] = login_response.parsed_response["access_token"]
end
logger.info session[:access_token]
# if login_response.parsed_response["access_token"] == nil
# session[:user_name] = "Guest"
# render 'index'
# return
# end
# user_id = login_response.parsed_response["access_token"]
# puts login_response
# # This feeds the user_id and incorrect oauth info in order to get the proper key returned to us by the server
# test = HTTParty.get("https://api.kitchology.com/api/v1/users/secure", :body =>
# {Authorization: "Oauth", oauth_consumer_key: user_id,
# oauth_signature: "LpT4O7lj2p01vvQ8lqaU+gNwMpM=",
# oauth_signature_method: "HMAC-SHA1", oauth_timestamp: "1411033900",
# oauth_nonce: "EhsUDPmwwoa"})
# puts test
# # This splits the error message and recieves the proper signature from the it.
# sig_string = test.parsed_response
# split_string = sig_string.split(/\s+/)
# signature = split_string[split_string.size-1]
# # We use the returned signature to make a proper oauth call and get the user information
# account_info = HTTParty.get("https://api.kitchology.com/api/v1/users/secure", :body =>
# {Authorization: "Oauth", oauth_consumer_key: user_id,
# oauth_signature: signature,
# oauth_signature_method: "HMAC-SHA1", oauth_timestamp: "1411033900",
# oauth_nonce: "EhsUDPmwwoa"})
# puts account_info
# # If we get an account information object we look for fields to display in descending order of value to the user.
# account_hash = account_info.parsed_response
# if account_hash["account"] != nil
# if account_hash["account"]["first_name"] != nil
# session[:user_name] = account_hash["account"]["name"]
# elsif account_hash["account"]["email"] != nil
# session[:user_name] = account_hash["account"]["email"]
# elsif account_hash["account"]["memberId"] != nil
# session[:user_name] = account_hash["account"]["memberId"]
# else
# session[:user_name] = 'User'
# end
# else
# session[:user_name] = 'User'
# end
rescue => exception
logger.info "***Exception during login: #{exception}"
flash[:notice] = 'Sign in failed! Ensure email/password are correct.'
end
if session[:access_token].nil?
flash[:notice] = 'Sign in failed! Ensure email/password are correct.'
# redirect_to :root
end
# Working curl for login
# curl -v --data "grant_type=password&[email protected]&password=KevinChugh" https://api.kitchology.com/api/v1/users/login
# {"access_token":"163","token_type":"mac","expires_in":3600,
# "mac_key":"17d49e8a9031c811e0dbe7872053c20a16f42351","mac_algorithm":"hmac-sha-1",
# "userName":"msc-admin","email":"[email protected]"}
end
def logout
session.delete(:user_name)
session.delete(:access_token)
redirect_to :root
end
def register
# If this call throws an exception it failed, otherwise it succeeded.
begin
logger.info "API string: email=#{params[:email]}&firstname=#{params[:first_name]}&lastname=#{params[:last_name]}&password=#{params[:password]}&username=#{params[:username]}&DOB=#{params[:dob]}&gender=#{params[:gender]}"
request = RestClient::Request.execute(:method => :post, :url => "https://dev.kitchology.com/api/v1/users/create", :payload => "email=#{params[:email]}&firstname=#{params[:first_name]}&lastname=#{params[:last_name]}&password=#{params[:password]}&username=#{params[:username]}&DOB=#{params[:dob]}&gender=#{params[:gender]}")
@success = true
rescue => exception
@success = false
logger.info "Error during registration: #{exception}"
end
# Latest
# curl -v -XPOST -H "Authoriza 163" -d '[email protected]&firstname=colinzz&lastname=fikzez&password=zSmoop123z&username=hahahahaz&DOB=06/30/1992&gender=female' 'https://dev.kitchology.com/api/v1/users/create'
logger.info params
logger.info "Response: #{request}"
# requset = RestClient::Request.execute(:method => :post, :url => "https://dev.kitchology.com/api/v1/users/create", :headers => { "Authorization" => "163"}, :payload => '[email protected]&firstname=qzxcasd&lastname=zxcv&password=Snepia15&username=poasd')
end
def forgot_password
# If this call throws an exception it failed, otherwise it succeeded.
begin
requset = RestClient::Request.execute(:method => :post, :url => "https://dev.kitchology.com/api/v1/users/reset_password", :payload => "email=#{params[:email]}")
@success = true
rescue => exception
@success = false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment