This gives us more flexibility and allows us to use password authentication later. Also we don't need to build the login functionality ourself.
35 lines
1.0 KiB
Ruby
35 lines
1.0 KiB
Ruby
class OauthsController < ApplicationController
|
|
|
|
# Sends the user on a trip to the provider,
|
|
# and after authorizing there back to the callback url.
|
|
def oauth
|
|
login_at(params[:provider])
|
|
end
|
|
|
|
def callback
|
|
provider = params[:provider]
|
|
if @user = login_from(provider)
|
|
redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
|
|
else
|
|
begin
|
|
@user = create_from(provider)
|
|
if authentication = @user.authentications.find_by(provider: provider)
|
|
authentication.update({
|
|
access_token: @access_token.token,
|
|
refresh_token: @access_token.refresh_token,
|
|
expires_at: Time.at(@access_token.expires_at)
|
|
})
|
|
end
|
|
|
|
reset_session
|
|
auto_login(@user)
|
|
redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
|
|
rescue
|
|
Rails.logger.error("Failed to login from #{provider}")
|
|
redirect_to root_path, :alert => "Failed to login from #{provider.titleize}!"
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|