hackatime/app/controllers/sessions_controller.rb
2025-02-17 18:54:28 -05:00

34 lines
1 KiB
Ruby

class SessionsController < ApplicationController
def new
redirect_uri = url_for(action: :create, only_path: false)
Rails.logger.info "Starting Slack OAuth flow with redirect URI: #{redirect_uri}"
redirect_to User.authorize_url(redirect_uri),
host: "https://slack.com",
allow_other_host: true
end
def create
redirect_uri = url_for(action: :create, only_path: false)
if params[:error].present?
Rails.logger.error "Slack OAuth error: #{params[:error]}"
redirect_to root_path, alert: "Failed to authenticate with Slack"
return
end
@user = User.from_slack_token(params[:code], redirect_uri)
if @user&.persisted?
session[:user_id] = @user.id
redirect_to root_path, notice: "Successfully signed in with Slack!"
else
Rails.logger.error "Failed to create/update user from Slack data"
redirect_to root_path, alert: "Failed to sign in with Slack"
end
end
def destroy
session[:user_id] = nil
redirect_to root_path, notice: "Signed out!"
end
end