mirror of
https://github.com/System-End/hackatime.git
synced 2026-04-20 00:35:22 +00:00
34 lines
1.1 KiB
Ruby
34 lines
1.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", error: @user.errors.full_messages
|
|
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
|