35 lines
1.1 KiB
Ruby
35 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
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.zone.at(@access_token.expires_at)
|
|
})
|
|
end
|
|
|
|
reset_session
|
|
auto_login(@user)
|
|
redirect_to root_path, notice: "Logged in from #{provider.titleize}!"
|
|
rescue StandardError
|
|
Rails.logger.error("Failed to login from #{provider}")
|
|
redirect_to root_path, alert: "Failed to login from #{provider.titleize}!"
|
|
end
|
|
end
|
|
end
|
|
end
|