discourse/app/controllers/facebook_controller.rb

94 lines
2.2 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
class FacebookController < ApplicationController
skip_before_filter :check_xhr, only: [:frame, :complete]
layout false
2013-02-07 10:45:24 -05:00
def frame
2013-02-05 14:16:51 -05:00
redirect_to oauth_consumer.url_for_oauth_code(:permissions => "email")
end
def complete
consumer = oauth_consumer
token = consumer.get_access_token(params[:code])
graph = Koala::Facebook::API.new(token)
me = graph.get_object("me")
email = me["email"]
verified = me["verified"]
name = me["name"]
username = User.suggest_username(me["username"])
verified = me["verified"]
# non verified accounts are just trouble
unless verified
2013-02-07 10:45:24 -05:00
render text: "Your account must be verified with facebook, before authenticating with facebook"
2013-02-05 14:16:51 -05:00
return
end
session[:authentication] = {
facebook: {
facebook_user_id: me["id"],
link: me["link"],
username: me["username"],
first_name: me["first_name"],
last_name: me["last_name"],
email: me["email"],
gender: me["gender"],
name: me["name"]
},
email: me["email"],
email_valid: true
}
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
user_info = FacebookUserInfo.where(:facebook_user_id => me["id"]).first
@data = {
username: username,
name: name,
email: email,
auth_provider: "Facebook",
email_valid: true
}
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
if user_info
user = user_info.user
if user
unless user.active
2013-02-07 10:45:24 -05:00
user.active = true
2013-02-05 14:16:51 -05:00
user.save
end
log_on_user(user)
@data[:authenticated] = true
end
2013-02-07 10:45:24 -05:00
else
2013-02-05 14:16:51 -05:00
user = User.where(email: me["email"]).first
if user
FacebookUserInfo.create!(session[:authentication][:facebook].merge(user_id: user.id))
unless user.active
2013-02-07 10:45:24 -05:00
user.active = true
2013-02-05 14:16:51 -05:00
user.save
end
log_on_user(user)
@data[:authenticated] = true
end
end
end
2013-02-07 10:45:24 -05:00
protected
2013-02-05 14:16:51 -05:00
def oauth_consumer
require 'koala'
2013-02-07 10:45:24 -05:00
host = request.host
2013-02-05 14:16:51 -05:00
host = "#{host}:#{request.port}" if request.port != 80
callback_url = "http://#{host}/facebook/complete"
2013-02-07 10:45:24 -05:00
2013-02-05 14:16:51 -05:00
oauth = Koala::Facebook::OAuth.new(SiteSetting.facebook_app_id, SiteSetting.facebook_app_secret, callback_url)
end
end