diff --git a/.idea/price-point-api.iml b/.idea/price-point-api.iml index a12de13..768c6ff 100644 --- a/.idea/price-point-api.iml +++ b/.idea/price-point-api.iml @@ -41,6 +41,7 @@ + @@ -56,6 +57,7 @@ + @@ -68,8 +70,11 @@ + + + @@ -88,6 +93,7 @@ + @@ -110,9 +116,11 @@ + + @@ -122,6 +130,7 @@ + @@ -129,6 +138,7 @@ + @@ -138,6 +148,9 @@ + + + diff --git a/Gemfile b/Gemfile index 4b161c9..06bb567 100644 --- a/Gemfile +++ b/Gemfile @@ -49,4 +49,7 @@ group :development, :test do gem "annotaterb" gem "dotenv-rails" -end \ No newline at end of file + + # Ruby language server for IDE support + gem "solargraph", require: false +end diff --git a/Gemfile.lock b/Gemfile.lock index 7c74c6d..b30eebc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -76,6 +76,7 @@ GEM activerecord (>= 6.0.0) activesupport (>= 6.0.0) ast (2.4.3) + backport (1.2.0) base64 (0.3.0) bcrypt (3.1.21) bcrypt_pbkdf (1.1.2) @@ -94,6 +95,7 @@ GEM debug (1.11.1) irb (~> 1.10) reline (>= 0.3.8) + diff-lcs (1.6.2) dotenv (3.2.0) dotenv-rails (3.2.0) dotenv (= 3.2.0) @@ -117,6 +119,7 @@ GEM prism (>= 1.3.0) rdoc (>= 4.0.0) reline (>= 0.4.2) + jaro_winkler (1.6.1) json (2.19.5) kamal (2.11.0) activesupport (>= 7.0) @@ -129,6 +132,10 @@ GEM sshkit (>= 1.23.0, < 2.0) thor (~> 1.3) zeitwerk (>= 2.6.18, < 3.0) + kramdown (2.5.1) + rexml (>= 3.3.9) + kramdown-parser-gfm (1.1.0) + kramdown (~> 2.0) language_server-protocol (3.17.0.5) lint_roller (1.1.0) logger (1.7.0) @@ -176,6 +183,7 @@ GEM racc (~> 1.4) nokogiri (1.19.3-x86_64-linux-musl) racc (~> 1.4) + observer (0.1.2) ostruct (0.6.3) parallel (2.1.0) parser (3.3.11.1) @@ -243,6 +251,8 @@ GEM zeitwerk (~> 2.6) rainbow (3.1.1) rake (13.4.2) + rbs (3.9.5) + logger rdoc (7.2.0) erb psych (>= 4.0.0) @@ -250,6 +260,8 @@ GEM regexp_parser (2.12.0) reline (0.6.3) io-console (~> 0.5) + reverse_markdown (3.0.0) + nokogiri rexml (3.4.4) rubocop (1.86.2) json (~> 2.3) @@ -283,6 +295,27 @@ GEM rexml ruby-progressbar (1.13.0) securerandom (0.4.1) + solargraph (0.57.0) + backport (~> 1.2) + benchmark (~> 0.4) + bundler (~> 2.0) + diff-lcs (~> 1.4) + jaro_winkler (~> 1.6, >= 1.6.1) + kramdown (~> 2.3) + kramdown-parser-gfm (~> 1.1) + logger (~> 1.6) + observer (~> 0.1) + ostruct (~> 0.6) + parser (~> 3.0) + prism (~> 1.4) + rbs (>= 3.6.1, <= 4.0.0.dev.4) + reverse_markdown (~> 3.0) + rubocop (~> 1.76) + thor (~> 1.0) + tilt (~> 2.0) + yard (~> 0.9, >= 0.9.24) + yard-activesupport-concern (~> 0.0) + yard-solargraph (~> 0.1) solid_cable (3.0.12) actioncable (>= 7.2) activejob (>= 7.2) @@ -312,6 +345,7 @@ GEM thruster (0.1.20-aarch64-linux) thruster (0.1.20-arm64-darwin) thruster (0.1.20-x86_64-linux) + tilt (2.6.1) timeout (0.6.1) tsort (0.2.0) tzinfo (2.0.6) @@ -325,6 +359,11 @@ GEM base64 websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) + yard (0.9.37) + yard-activesupport-concern (0.0.1) + yard (>= 0.8) + yard-solargraph (0.1.0) + yard (~> 0.9) zeitwerk (2.7.5) PLATFORMS @@ -351,6 +390,7 @@ DEPENDENCIES rails (~> 8.0.4) rails-erd rubocop-rails-omakase + solargraph solid_cable solid_cache solid_queue diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c1d2a38..a44778f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,12 +1,15 @@ -class ApplicationController < ActionController::API +module ApplicationController + extend ActionController::API before_action :authenticate! + rescue_from Unauthorized, with: -> { render json: { error: "Unauthorized" }, status: :unauthorized } + private def authenticate! token = request.headers["Authorization"]&.split(" ")&.last - @current_user = User.find_by(token:) - - render json: { error: "Unauthorized" }, status: :unauthorized unless @current_user + @current_user = Session.find_by(token:)&.user + + raise Unauthorized unless @current_user end def current_user diff --git a/app/controllers/concerns/renderable.rb b/app/controllers/concerns/renderable.rb new file mode 100644 index 0000000..86e8f4e --- /dev/null +++ b/app/controllers/concerns/renderable.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class Renderable + extend ActiveSupport::Concern + + def render_success(data = nil, status = :ok, meta = {}) + body = { success: true } + body[:data] = data if data + body[:meta] = meta if meta.present? + render json: body + end + + def render_error + + end + +end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..c1cf048 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,12 @@ +class SessionsController < ApplicationController + before_action :authenticate_user, only: [:destroy] + + def create + user = User.find_by(email: params[:email]) + + end + + def destroy + + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..8ae5318 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,13 @@ +class UsersController < ApplicationController + def create + + end + + def show + + end + + def destroy + + end +end diff --git a/app/errors/unauthorized.rb b/app/errors/unauthorized.rb new file mode 100644 index 0000000..549f1c4 --- /dev/null +++ b/app/errors/unauthorized.rb @@ -0,0 +1 @@ +class Unauthorized < StandardError; end diff --git a/config/routes.rb b/config/routes.rb index a568599..e16f176 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,11 @@ Rails.application.routes.draw do resources :products do resources :price_reports, only: [:index, :create] end + + post 'login', to: 'sessions#create' + delete 'logout', to: 'sessions#destroy' + post 'signup', to: 'users#create' + # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb new file mode 100644 index 0000000..e5da4b3 --- /dev/null +++ b/test/controllers/sessions_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class SessionsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..61c1532 --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UsersControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end