Guest User

Untitled

a guest
Jun 17th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. Allow the metal piece to run in isolation
  2. require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
  3. require 'sinatra'
  4.  
  5. class Auth < Sinatra::Application
  6. #enable :sessions
  7.  
  8. configure do
  9. FB = YAML.load_file(File.dirname(__FILE__) + "/../../config/facebooker.yml")
  10. end
  11.  
  12. helpers do
  13. # Load portal and setup facebook oauth
  14. def facebook_client
  15. portal = Portal.find(params[:portal_id])
  16. if RAILS_ENV == "production" && @portal.facebook_api_key
  17. facebook_connect = FB["production"]["alternative_keys"][portal.facebook_api_key]
  18. elsif request.host == '127.0.0.1' #debugging keys with callback domain 127.0.0.1
  19. facebook_connect = {"secret_key" => 'XXXXXXXX', "application_id" => 'XXXX' }
  20. portal = Portal.find_by_default_start_portal(true)
  21. else
  22. facebook_connect = FB[RAILS_ENV]
  23. end
  24. facebook_connect["application_id"] = portal.facebook_api_key unless facebook_connect["application_id"]
  25.  
  26. callback_url = "#{request.scheme}://#{request.host}:#{request.port}/auth/facebook/callback?portal_id=#{portal.id}"
  27. return FacebookOAuth::Client.new(
  28. :application_id => facebook_connect["application_id"],
  29. :application_secret => facebook_connect["secret_key"],
  30. :callback => callback_url)
  31. end
  32.  
  33. end
  34.  
  35. get '/auth/facebook' do
  36. client = facebook_client
  37. if params[:format] == "mobile"
  38. redirect "#{client.authorize_url}&display=touch"
  39. else
  40. redirect client.authorize_url
  41. end
  42. end
  43.  
  44. get '/auth/facebook/callback' do
  45. client = facebook_client
  46. access_token = client.authorize(:code => params[:code]) # it's possible to load data from facebook by this access_token.token
  47. info = client.me.info
  48.  
  49. current_user = User.find_by_fb_user_id(info["id"])
  50. if current_user
  51. session[:user_id] = current_user.id
  52. else
  53. current_user = User.new(
  54. :name => info["name"],
  55. :login => info["name"].downcase.gsub(" ","_"),
  56. :password => "", :email => "",
  57. :fb_user_id => info["id"],
  58. :portal_id => @portal.id,
  59. :original_potal_id => @portal.id)
  60. if current_user.save
  61. session[:user_id] = current_user.id
  62. end
  63. end
  64. redirect "/home"
  65. end
Add Comment
Please, Sign In to add comment