diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index f0d5b72..b066582 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -1,10 +1,10 @@ class MessagesController < ApplicationController include CompletionsConcern - skip_before_action :verify_authenticity_token, only: %i[ create cors_preflight_check ] - skip_before_action :require_authentication, only: %i[ create cors_preflight_check ] + skip_before_action :verify_authenticity_token, only: %i[ create connect cors_preflight_check ] + skip_before_action :require_authentication, only: %i[ create connect cors_preflight_check ] - before_action :cors_set_access_control_headers, only: %i[ create cors_preflight_check ] + before_action :cors_set_access_control_headers, only: %i[ create connect cors_preflight_check ] before_action :set_contest, only: %i[ convert destroy ] before_action :set_data, only: %i[ convert ] @@ -24,6 +24,27 @@ class MessagesController < ApplicationController skip_authorization end + def connect + skip_authorization + + if !params.key?(:token) + respond_to do |format| + format.json { render json: { error: "no token provided" }, status: 400 } + end + else + @contest = Contest.find_by_token_for(:token, params[:token]) + if @contest + respond_to do |format| + format.json { render json: { name: @contest.name }, status: 200 } + end + else + respond_to do |format| + format.json { render json: { error: "invalid token" }, status: 400 } + end + end + end + end + def create skip_authorization @@ -37,12 +58,12 @@ class MessagesController < ApplicationController end else respond_to do |format| - format.json { render json: { error: "invalid contest token" }, status: 400 } + format.json { render json: { error: "invalid token" }, status: 400 } end end rescue respond_to do |format| - format.json { render json: { error: "invalid contest token" }, status: 400 } + format.json { render json: { error: "invalid token" }, status: 400 } end end end diff --git a/config/routes.rb b/config/routes.rb index ff06d9a..fceb1ef 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,7 +24,9 @@ Rails.application.routes.draw do resource :session resources :users + options "connect", to: "messages#cors_preflight_check" options "message", to: "messages#cors_preflight_check" + post "connect", to: "messages#connect" post "message", to: "messages#create" get "public/:id", to: "contests#scoreboard"