diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index cccf977..a16afbb 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -2,7 +2,7 @@ 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), + redirect_to User.authorize_url(redirect_uri, close_window: params[:close_window].present?), host: "https://slack.com", allow_other_host: true end @@ -26,13 +26,22 @@ class SessionsController < ApplicationController OneTime::MigrateUserFromHackatimeJob.perform_later(@user.id) end - redirect_to root_path, notice: "Successfully signed in with Slack!" + state = JSON.parse(params[:state]) rescue {} + if state["close_window"] + redirect_to close_window_path + else + redirect_to root_path, notice: "Successfully signed in with Slack!" + end 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 close_window + render :close_window, layout: false + end + def github_new unless current_user redirect_to root_path, alert: "Please sign in first to link your GitHub account" diff --git a/app/models/user.rb b/app/models/user.rb index bb30e1a..19c881d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -183,11 +183,16 @@ class User < ApplicationRecord }) end - def self.authorize_url(redirect_uri) + def self.authorize_url(redirect_uri, close_window: false) + state = { + token: SecureRandom.hex(24), + close_window: close_window + }.to_json + params = { client_id: ENV["SLACK_CLIENT_ID"], redirect_uri: redirect_uri, - state: SecureRandom.hex(24), + state: state, user_scope: "users.profile:read,users.profile:write,users:read,users:read.email" } diff --git a/app/views/sessions/close_window.html.erb b/app/views/sessions/close_window.html.erb new file mode 100644 index 0000000..e333ebf --- /dev/null +++ b/app/views/sessions/close_window.html.erb @@ -0,0 +1,7 @@ +<% content_for :title, "Successfully signed in!" %> + +

Successfully signed in! You can close this window.

diff --git a/config/routes.rb b/config/routes.rb index f53eaa2..bd08d67 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,6 +50,7 @@ Rails.application.routes.draw do get "/auth/github/callback", to: "sessions#github_create" post "/auth/email", to: "sessions#email", as: :email_auth get "/auth/token/:token", to: "sessions#token", as: :auth_token + get "/auth/close_window", to: "sessions#close_window", as: :close_window delete "signout", to: "sessions#destroy", as: "signout" resources :leaderboards, only: [ :index ]