Multiple namespaces for the same Model

738 vistas
Ir al primer mensaje no leído

Nic

no leída,
21 abr 2012, 12:42:00 a.m.21/4/12
para Devise
I have two namespaces: an API namespace, and the default namespace,
but the two seem to be conflicting when a user tries to sign in.
Meaning, whichever namespace that references Devise first ends up
being the final redirection place. If I try to create a new session
under the default namespace it will fail on that user session path,
and then attempt to create the session on the API/v1 sessions path.

How do I make the two act independently?

They are both referencing a User object. The user_sessions controller
for the default namespace is 'user_sessions'. The user_sessions
controller for the API/V1 namespace is '/api/v1/user_sessions'

---- ROUTES.RB -------
MySite::Application.routes.draw do
namespace :api do
namespace :v1 do
devise_for :users,:controllers => { :sessions => "api/v1/
user_sessions",:registrations=>"users" }
......
end
end
devise_for :users,:controllers => { :sessions =>
"user_sessions",:registrations=>"users" } do
post 'users/sign_in' => 'user_sessions#create', :as
=> :user_session
get 'users/sign_in' => 'user_sessions#new', :as
=> :new_user_session
get 'users/sign_up' => 'user_sessions#new', :as
=> :new_user_session
match 'users/sign_out' => 'user_sessions#destroy', :as
=> :destroy_user_session
<.....>
end
----- DEFAULT NAMESPACE USER_SESSIONS_CONTROLLER -----
class UserSessionsController < Devise::SessionsController
....
end
---- API NAMESPACE USER_SESSIONS_CONTROLLER ---- (this goes to my
custom Devise base controller marked below)
class Api::V1::UserSessionsController < Api::V1::DeviseBaseController
...
end
---- API NAMESPACE CUSTOM DEVISE BASE CONTROLLER ----
class Api::V1::DeviseBaseController < Devise::SessionsController
respond_to :json

end

Nic Werner

no leída,
6 jun 2012, 5:25:15 p.m.6/6/12
para plataforma...@googlegroups.com
Dennis, I didn't find a true solution. I read somewhere that Devise is namespace-agnostic and that there is only one instance. What I ended up doing was keeping my two namespaces, but only having devise in the original namespace. Then I pointed all auth requests to that Devise controller and routed appropriately.

This may not work for your situation, but since I'm using cancan for authorization (in separate namespaces) that allowed me to mitigate the namespace overlap to only Devise-related actions.

If you do find a solution, I'd love to hear it :)

- Nic


On Wed, Jun 6, 2012 at 2:07 PM, Dennis <drb...@gmail.com> wrote:
Hi Nic,

I'm having the exact same problem. Trying to figure it out, but with no luck so far.

Will continue trying, if I find anything, I'll share it.

Regards,

Dennis

Op zaterdag 21 april 2012 06:42:00 UTC+2 schreef Nic het volgende:

Dennis

no leída,
6 jun 2012, 6:43:43 p.m.6/6/12
para Devise
Hi Nic,

I've come up with a solution for my case. See https://gist.github.com/1255275#gistcomment-344355
.

In short, the Api namespace I use is 100% custom, just thought adding
devise to it (just like the default), would save me time. The routes
didn't, but I do use the Devise helpers to authenticate using token.

If you'd like more info, just shoot me a message.

Regards,

Dennis
> >> MySite::Application.routes.**draw do
> >> namespace :api do
> >>     namespace :v1 do
> >>       devise_for :users,:controllers => { :sessions => "api/v1/
> >> user_sessions",:registrations=**>"users" }
> >>       ......
> >>     end
> >>   end
> >>       devise_for :users,:controllers => { :sessions =>
> >> "user_sessions",:**registrations=>"users" } do
> >>      post 'users/sign_in' => 'user_sessions#create', :as
> >> => :user_session
> >>      get 'users/sign_in' => 'user_sessions#new', :as
> >> => :new_user_session
> >>      get 'users/sign_up' => 'user_sessions#new', :as
> >> => :new_user_session
> >>      match 'users/sign_out' => 'user_sessions#destroy', :as
> >> => :destroy_user_session
> >>      <.....>
> >>  end
> >> ----- DEFAULT NAMESPACE USER_SESSIONS_CONTROLLER -----
> >> class UserSessionsController < Devise::SessionsController
> >> ....
> >> end
> >> ---- API NAMESPACE USER_SESSIONS_CONTROLLER ---- (this goes to my
> >> custom Devise base controller marked below)
> >> class Api::V1::**UserSessionsController < Api::V1::DeviseBaseController
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos