Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide a route for current user in a REST API?

So I have a simple users resource defined in my routes.

resources :users

I need to provide a route so that a logged in user can get his information in the front end. I thought about doing this:

resources :users do
  get 'current' , on: :collection
end

But this semantically means that the route gives a list of current users, not the current user. And Indeed if I do rake routes, it shows the path prefix as: current_users

I can work around the path prefix by doing something like this:

get '/users/current' => 'users#current', as: 'current_user'

But this still looks like it provides a list of users. So, I was wondering if there was a 'right way' to do this kind of thing and how to do it in rails? Or am I thinking too much about this and what I have is fine?

like image 985
Amr Noman Avatar asked Sep 08 '25 02:09

Amr Noman


2 Answers

if there was a 'right way'

The "right" way is to use a singular resource:

#config/routes.rb
resource :user #-> url.com/user -> users#show

Of course, the "right" way implies there is a "wrong" way, which is not the case.

Programming is one of the few practices where you can do whatever you want, so long as the result is achieved. Of course, how you achieve that result can be criticized; ultimately, there are a myriad of ways to achieve it...

#config/routes.rb
resources :users do
  get :current, on: :collection #-> url.com/users/current -> users#current
end

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def current
      @user = current_user
   end
end 

The above (your code) does what my code does, just in a different way.

Just because you're using a collection route, doesn't mean you need to pass a collection. The "collection" route is mainly for Rails routing structures; the routes have no bearing on your backend code.

--

This also means you should be willing to experiment with options / switches to customize your routing:

#config/routes.rb
resource :user, path: "current_user", as: :current_user #-> url.com/current_user -> users#show
like image 93
Richard Peck Avatar answered Sep 09 '25 15:09

Richard Peck


2.5 Singular Resources Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action: http://guides.rubyonrails.org/routing.html#singular-resources

maybe you meant it?

resource :user
like image 30
henb Avatar answered Sep 09 '25 14:09

henb