-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OmniAuth with Locale #2813
Comments
The solution you found is correct. The thing is that omniauth does not support dynamic paths, so you can't have locale in it. Calling Devise twice or defining the omniauth routes directly is the way to go. :) |
@kot-begemot It's a while ago you came up with this problem now I was facing too. I think I found an elegant solution for this. First we need a controller inside the locale scope that sets us the current locale in the session. Rails.application.routes.draw do
# We need to define devise_for just omniauth_callbacks:uth_callbacks otherwise it does not work with scoped locales
# see https://github.com/plataformatec/devise/issues/2813
devise_for :users, skip: [:session, :password, :registration, :confirmation], controllers: { omniauth_callbacks: 'omniauth_callbacks' }
scope '(:locale)' do
# We define here a route inside the locale thats just saves the current locale in the session
get 'omniauth/:provider' => 'omniauth#localized', as: :localized_omniauth
devise_for :users, skip: :omniauth_callbacks, controllers: { passwords: 'passwords', registrations: 'registrations' }
end
end
class OmniauthController < ApplicationController
def localized
# Just save the current locale in the session and redirect to the unscoped path as before
session[:omniauth_login_locale] = I18n.locale
redirect_to user_omniauth_authorize_path(params[:provider])
end
end Now we can force the locle inside the class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def twitter
handle_redirect('devise.twitter_uid', 'Twitter')
end
def facebook
handle_redirect('devise.facebook_data', 'Facebook')
end
private
def handle_redirect(_session_variable, kind)
# here we force the locale to the session locale so it siwtches to the correct locale
I18n.locale = session[:omniauth_login_locale] || I18n.default_locale
sign_in_and_redirect user, event: :authentication
set_flash_message(:notice, :success, kind: kind) if is_navigational_format?
end
def user
User.find_for_oauth(env['omniauth.auth'], current_user)
end
end That's all, everytime you login via Also as gist https://gist.github.com/gurix/4ed589b5551661c1536a |
I added @gurix solution to the wiki: https://github.com/plataformatec/devise/wiki/How-To:-OmniAuth-inside-localized-scope |
I used your solution for a while, but seem to bump into one issue lately. Apparently you cannot call A solution to this problem is to redeclare the omniauth controller in the second If you want to namespace all controllers under Rails.application.routes.draw do
devise_for :users, skip: [:sessions, :registrations, :passwords, :unlocks, :confirmations], controllers:{
omniauth_callbacks: 'users/omniauth_callbacks'
}
localized do
devise_for :users, skip: :omniauth_callbacks, controllers: {
sessions: 'users/sessions',
registrations: 'users/registrations',
passwords: 'users/passwords',
unlocks: 'users/unlocks',
confirmations: 'users/confirmations',
omniauth_callbacks: 'users/omniauth_callbacks'
}
end
end |
Hey. I am developing rails-localization gem. Everything went pretty well, until I tried to use Devise + OmniAuth inside localized scope.
tldr
The solution I found is to use
devise_for :user
two times inside routes.rbBut I am not sure that this is a good practice and I would like to hear a better option.
The problem
Point is, this scope yields content like that
All other URL are happy with that, but not if you use omniauth with devise_for
Then, Rails will fail on initialization with that error.
So far so good. I tried to implement it like that
It failed with that error.
Next attempt was that one
And test failed =(. Here is its output
And here is test file content
Just to mention, that this test IS PASSING if I am defining
devise_for
outside oflocalized
scopeSome of my playing arounds
I thought that root of that
Not found. Authentication passthru.
problem is that Devise.mappings[:user] is missing those thingsAnd the black magic, somewhere inside
Devise::OmniauthCallbacksController
is checking for those things, so I tried to insert those things into Mapping instance manuallyAnd the did not worked out =(
The text was updated successfully, but these errors were encountered: