Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise+Omniauth, routes versioning

I have a model Candidate which is devise omniauthable (linkedin).

So far, my routes.rb looked like this :

namespace :v1 do
    devise_for :candidates, only: :omniauth_callbacks
    ...
end

Everything worked well till I had to add a new version :

namespace :v2 do
    devise_for :candidates, only: :omniauth_callbacks
    ...
end

namespace :v1 do
    devise_for :candidates, only: :omniauth_callbacks
    ...
end

With the current configuration, I get this error :

`set_omniauth_path_prefix!': Wrong OmniAuth configuration. If you are getting this exception, it means that either: (RuntimeError)
1) You are manually setting OmniAuth.config.path_prefix and it doesn't match the Devise one
2) You are setting :omniauthable in more than one model
3) You changed your Devise routes/OmniAuth setting and haven't restarted your server

It's kind of annoying since I want to be able to authenticate the candidate on both versions.

What can I do ?

like image 238
Jérôme Boé Avatar asked Jul 04 '14 12:07

Jérôme Boé


1 Answers

Alright, let's recap a little bit here, Devise doesn't allow you to call the devise_for method inside a scope or a namespace route defined in the config/routes.rb file, right?

My namespace'd route looks like this:

namespace :api, constraints: { format: :json } do
  devise_for :users, skip: [ :registrations, :passwords, :confirmations ]
  resources :profiles, only: :show
end

And it works!

What did I do to make it work? the answer lies in the config/initializers/devise.rb file. Check out near the bottom of the File it says...

# When using omniauth, Devise cannot automatically set Omniauth path, # so you need to do it manually. For the users scope, it would be:

The next commented line shows you an example, uncomment that line and modify it according to your needs, for my case(ie. for the namespaced route I have above) I have:

config.omniauth_path_prefix = "/api/users/auth"

And that's it! .... I did that and it all started to work perfectly!

Hope it helps!

like image 96
jlstr Avatar answered Oct 12 '22 16:10

jlstr