diff --git a/backend/app/controllers/application_controller.rb b/backend/app/controllers/application_controller.rb index dd8dd73..a3d1132 100644 --- a/backend/app/controllers/application_controller.rb +++ b/backend/app/controllers/application_controller.rb @@ -2,4 +2,10 @@ class ApplicationController < ActionController::Base before_action :set_paper_trail_whodunnit + + def after_sign_in_path_for(resource) + return admin_root_path if resource.is_a?(User) && resource.admin? + + super + end end diff --git a/backend/app/dashboards/user_dashboard.rb b/backend/app/dashboards/user_dashboard.rb index 18355ac..60281dc 100644 --- a/backend/app/dashboards/user_dashboard.rb +++ b/backend/app/dashboards/user_dashboard.rb @@ -10,7 +10,7 @@ class UserDashboard < Administrate::BaseDashboard # which determines how the attribute is displayed # on pages throughout the dashboard. ATTRIBUTE_TYPES = { - id: Field::Number, + id: Field::String, admin: Field::Boolean, email: Field::String, password: Field::String, diff --git a/backend/spec/requests/sessions_spec.rb b/backend/spec/requests/sessions_spec.rb new file mode 100644 index 0000000..01c9399 --- /dev/null +++ b/backend/spec/requests/sessions_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe "Sessions" do + describe "POST /users/sign_in" do + let(:password) { "password" } + + context "when the user is an admin" do + let(:user) { create(:user, admin: true, password: password) } + + before do + post user_session_path, params: { user: { email: user.email, password: password } } + end + + it { expect(response).to redirect_to(admin_root_path) } + end + + context "when the user is not an admin" do + let(:user) { create(:user, password: password) } + + before do + post user_session_path, params: { user: { email: user.email, password: password } } + end + + it { expect(response).not_to redirect_to(admin_root_path) } + end + end +end