Devise change the user_omniauth_callback route - ruby-on-rails

I am working with OmniAuth to use Facebook Connect in my Devise based rails app. One of the routes it creates is:
user_omniauth_callback /users/auth/:action/callback(.:format) {:action=>/facebook/, :controller=>"devise/omniauth_callbacks"}
I'd like to modify this route to a custom URL. Where would be the right place to do that?
the problem is by default, the route it creates is http://foo/users/auth/:action/callback.format. I want to have something more custom like http://foo/prefix_path/users/auth/:action/callback.format. I tried making my routes file look like the following:
scope "/mypath" do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
end
but it still generates the wrong route:
user_omniauth_callback /users/auth/:action/callback(.:format) {:action=>/facebook/, :controller=>"users/omniauth_callbacks"}

I'm not exactly sure what you are asking, I assume you want to have your own custom code for the callback.
You can extend the devise controller such as:
class MyOmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
#Custom Code here
end
end
Then you can register this new controller in your routes.rb
devise_for :users, :controllers => {:omniauth_callbacks => "my_omniauth_callbacks"}
EDIT:
devise can also take a 'path' option in the devise_for so changing the route:
devise_for :users, :controllers => {:omniauth_callbacks => "my_omniauth_callbacks"}, :path => "path_prefix/users"

If you are unsatisfied with omniauthable in devise itself, then you may consider implementing omniauth as separate gem and then just tie it with device.
To modify routes, you may use :match as well and map those routes to omniauth_callbacks url. Didn't get why you want to
I'd like to modify this route to a custom URL.
Decribe what you want to make different that what is available.

Related

add your own custom route to devise

i've seen a bunch of posts on how to rename already-declared routes in devise. I want to expand devise to have my own route check for and idle session. I am implementing a simple js check every 1 minute that I want to hit 'check_active' in the devise sessions controller. I tried this but no luck:
devise_scope :sessions do
get 'check_active'
end
Is there way to expand devise with a custom route (not rename an already-existing one) ?
UPDATE - almost there, i did this
# already had this in routes
devise_for :users, :controllers =>
{ registrations: 'registrations',
confirmations: 'confirmations',
sessions: 'sessions',
passwords: 'passwords',
omniauth_callbacks: "omniauth_callbacks"}
# added this
devise_scope :sessions do
get '/check_active' => 'sessions#check_active'
end
I have a js firing, i have it get '/check_active' as rake routes shows this:
check_active GET /check_active(.:format)
But when it fires, the controller 404s with
AbstractController::ActionNotFound (Could not find devise mapping for path "/check_active".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
get "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
#request.env["devise.mapping"] = Devise.mappings[:user]
):
If you are overwriting Devise's default controllers, it is not any different from any other controller to add your own route.
After you create your devise controllers to overwrite, do the following:
Under sessions_controller declare a method
# app/controllers/devise/sessions_controller.rb
def check_active
# do what you want to do
end
And in your router:
# config/routes.rb
devise_scope :sessions do
get 'check_active', to: "devise/sessions#check_active"
end
I was trying the same thing and realized that the scope should be for user and not sessions, also ensure that it has to be singular.
devise_scope :user do
get '/check_active' => 'sessions#check_active'
end
Edit: Adding link to help docs for better understanding

Change Devise default route from provider method to create method in controller while using Omniauth

I'm trying to use Devise and Omniauth to allow a user to sign in to multiple providers with a Rails 3.2 app. I got it to work but only if it goes to a twitter, facebook or tumblr method in the controller where I want all the providers to go to a create method. This is what I have the routes to do this: match '/auth/:provider/callback' => 'authentications#create'
resources :authentications
match '/auth/:provider/callback' => 'authentications#create'
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "authentications"}
root to: 'authentications#index'
My rake routes show:
user_omniauth_authorize /users/auth/:provider(.:format) authentications#passthru {:provider=>/twitter|facebook|tumblr/}
user_omniauth_callback /users/auth/:action/callback(.:format) authentications#(?-mix:twitter|facebook|tumblr)
I want the user_omni_auth_callback route to to to authentications#create and not the specific providers. With the current setup, I get "The action 'twitter' could not be found for AuthenticationsController" because I'm not including it. What's the best way to do this?

Devise Registration URL could not be found

I have a problem with devise after customizing the signup - route.
The devise doc mentions, that routes can easily be customized, so I tried to add a token to the URL to set up a easy invitation system. Ist really straight forward and all I did was adding
devise_for :users, :path_names => { :sign_up => "signup/:invitation_token" }
to my routes. A mailer sends an email with the token and inside I pass
new_user_registration_path(#invitation.token)
rake routes says
new_user_registration GET /users/signup/:invitation_token(.:format) devise/registrations#new
But I'm still getting
No route matches {:action=>"new", :controller=>"devise/registrations", :locale=>:de, :invitation_token=>nil}
I get it wether I pass the token or not...
I'm not shure what I'm missing.
Thanks in advance, hope someones sees what I'm doing wrong.
Greets, Rob
Check #invitation.token to make sure that it's not nil.
The error you're witnessing will occur on any view in which you pass nil into your new_user_registration_path link tag.
Bear in mind that you'll need to override the default behavior of Devise's users/registration controller in order to get your invitation system working correctly. Something like this would work:
# routes.rb
devise_for :users, :path_names => { :sign_up => "signup/:invitation_token" }, :controllers => {:registrations => "users/registrations"}
# app/controllers/users/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
# add custom create logic here
end
end

Rails Devise user controller in subfolder

I am using omniauth and found devise using a subfolder for this(in official example) controllers/users/omniauth_callbacks_controller.rb. I need to create a User show page as well as other actions for User so I decide to create a new UsersController inside a controllers/users folder. Now it looks like
class Users::UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
end
routes.rb
My::Application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
match 'users/:id' => 'users/users#show'
root :to => 'home#index'
end
it works but the route created is unnamed
rake routes gives
/users/:id(.:format) users/users#show
without GET and route_name
so I'm unable to use it for example after login redirect. Is there a better way to realize the subfolder routes structure and is it good idea to group controllers like this?
You just need name your route in your route.rb
match 'users/:id' => 'users/users#show', :as => 'user'
After that you can call this route by user_url(user.id)
See example on guides : http://guides.rubyonrails.org/routing.html#naming-routes

Devise to show user id in the URL?

I am using Devise for authentication and I want to have URLs of the form /user/:id but I am not sure how to do this.
I guess I need to edit the routes file but not sure what change to make? Is there a simple way to do this?
My routes.rb file looks like this:
devise_for :users, :controllers => {:registrations => 'registrations'}
Thanks

Resources