Skip to content

Commit

Permalink
Update authorization into it's own anchor-link
Browse files Browse the repository at this point in the history
  • Loading branch information
scart88 authored Sep 29, 2024
1 parent 24ce03e commit 519de0f
Showing 1 changed file with 57 additions and 53 deletions.
110 changes: 57 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,63 +55,67 @@ To install Spectator Sport in your Rails application:
<script defer src="/spectator_sport/events.js"></script>
</head>
```
3. You must manually install and set up authorization for the Player Dashboard. You probably shouldn't make it public. If you are using Devise, authorizing admins might look like this:
```ruby
# config/routes.rb
authenticate :user, ->(user) { user.admin? } do
mount SpectatorSport::Dashboard::Engine, at: 'spectator_sport_dashboard', as: :spectator_sport_dashboard
end
```
## Authorization
It is advisable to manually install and set up authorization for the **Player Dashboard** and refrain from making it public.
If you are using Devise, the process of authorizing admins might resemble the following:
```ruby
# config/routes.rb
authenticate :user, ->(user) { user.admin? } do
mount SpectatorSport::Dashboard::Engine, at: 'spectator_sport_dashboard', as: :spectator_sport_dashboard
end
```
Or set up Basic Auth:
```ruby
# config/initializers/spectator_sport.rb
SpectatorSport::Dashboard::Engine.middleware.use(Rack::Auth::Basic) do |username, password|
ActiveSupport::SecurityUtils.secure_compare(Rails.application.credentials.spectator_sport_username, username) &
ActiveSupport::SecurityUtils.secure_compare(Rails.application.credentials.spectator_sport_password, password)
Or set up Basic Auth:
```ruby
# config/initializers/spectator_sport.rb
SpectatorSport::Dashboard::Engine.middleware.use(Rack::Auth::Basic) do |username, password|
ActiveSupport::SecurityUtils.secure_compare(Rails.application.credentials.spectator_sport_username, username) &
ActiveSupport::SecurityUtils.secure_compare(Rails.application.credentials.spectator_sport_password, password)
end
```
If you are using an authentication method similar to the one used in ONCE products, you can utilize an auth constraint in your routes.
```ruby
# config/routes.rb
class AuthRouteConstraint
def matches?(request)
return false unless request.session[:user_id]
user = User.find request.session[:user_id]
if user && user.admin?
cookies = ActionDispatch::Cookies::CookieJar.build(request, request.cookies)
token = cookies.signed[:session_token]
return user.sessions.find_by(token: token)
end
```
end
end
Rails.application.routes.draw do
namespace :admin, :constraints => AuthRouteConstraint.new do
mount SpectatorSport::Dashboard::Engine, at: 'spectator_sport_dashboard', as: :spectator_sport_dashboard
end
```
Or extend the `SpectatorSport::Dashboard::ApplicationController` with your own authorization logic:
```ruby
# config/initializers/good_job.rb
ActiveSupport.on_load(:spectator_sport_dashboard_application_controller) do
# context here is SpectatorSport::Dashboard::ApplicationController
before_action do
raise ActionController::RoutingError.new('Not Found') unless current_user&.admin?
end
def current_user
# load current user from session, cookies, etc.
end
end
```
If you are using an authentication similar to the one used in ONCE products, you can use an auth constraint in your routes:
```ruby
# config/routes.rb
class AuthRouteConstraint
def matches?(request)
return false unless request.session[:user_id]
user = User.find request.session[:user_id]
if user && user.admin?
cookies = ActionDispatch::Cookies::CookieJar.build(request, request.cookies)
token = cookies.signed[:session_token]
return user.sessions.find_by(token: token)
end
end
end
Rails.application.routes.draw do
namespace :admin, :constraints => AuthRouteConstraint.new do
mount SpectatorSport::Dashboard::Engine, at: 'spectator_sport_dashboard', as: :spectator_sport_dashboard
end
```
Or extend the `SpectatorSport::Dashboard::ApplicationController` with your own authorization logic:
```ruby
# config/initializers/good_job.rb
ActiveSupport.on_load(:spectator_sport_dashboard_application_controller) do
# context here is SpectatorSport::Dashboard::ApplicationController
before_action do
raise ActionController::RoutingError.new('Not Found') unless current_user&.admin?
end
def current_user
# load current user from session, cookies, etc.
end
end
```
## Contributing
Expand Down

0 comments on commit 519de0f

Please sign in to comment.