Add close_window param for Athena Awards

This commit is contained in:
Max Wofford 2025-04-29 11:13:05 -04:00
parent 5be0fa55ec
commit 5acd30f6f6
4 changed files with 26 additions and 4 deletions

View file

@ -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"

View file

@ -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"
}

View file

@ -0,0 +1,7 @@
<% content_for :title, "Successfully signed in!" %>
<script>
setTimeout(function() {
window.close();
}, 1000);
</script>
<p>Successfully signed in! You can close this window.</p>

View file

@ -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 ]