Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions app/controllers/api/v1/api_controller.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion app/controllers/api/v1/auth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ 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"]}
]},
status: 401
end
end


end
17 changes: 13 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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