diff --git a/app/controllers/api/v1/api_controller.rb b/app/controllers/api/v1/api_controller.rb new file mode 100644 index 0000000..1fcb0a6 --- /dev/null +++ b/app/controllers/api/v1/api_controller.rb @@ -0,0 +1,32 @@ +class Api::V1::ApiController < Api::ApiController + + #Security Token Checker + def checkToken + user = User.find_by(auth_token: request.headers["token"]) + if user.nil? + render json: {error: '401 Unauthorized'} , status: 401 + else + return true + end + end + + #product methods + before_action :checkToken + def productAll + products = Product.all + render json: {success: true, products: products} , status: 200 + end + + before_action :checkToken + def productSingle + product = Product.find(params[:id]) + render json: {success: true, products: [product]} , status: 200 + end + + #user methods + before_action :checkToken + def profile + user = User.find_by(auth_token: request.headers["token"]) + render json: {success: true, user: user } , status: 200 + end +end \ No newline at end of file diff --git a/app/controllers/api/v1/auth_controller.rb b/app/controllers/api/v1/auth_controller.rb index 20ef883..103f3ac 100644 --- a/app/controllers/api/v1/auth_controller.rb +++ b/app/controllers/api/v1/auth_controller.rb @@ -8,7 +8,8 @@ def create end if token - render json: {success: true, token: token} + render json: {success: true, token: token}, + status: 200 else render json: {success: false, errors: [ {user: ["Invalid password"]} @@ -16,4 +17,6 @@ def create status: 401 end end + + end diff --git a/config/routes.rb b/config/routes.rb index b36d403..2e916ee 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,10 +19,19 @@ end end + #API REST namespace :api, format: 'json' do - namespace :v1 do - post "auth", to: "auth#create" - get "locales", to: "locales#index", as: "locales" - end + namespace :v1 do + post "auth", to: "auth#create" + get "locales", to: "locales#index", as: "locales" + + #product routes + get "product", to: "api#productAll" + get 'product/:id', to: "api#productSingle" + + #user routes + get "/account", to: "api#profile" + end end + end