Skip to content

Instantly share code, notes, and snippets.

@gurix
Created August 21, 2014 14:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gurix/4ed589b5551661c1536a to your computer and use it in GitHub Desktop.
Save gurix/4ed589b5551661c1536a to your computer and use it in GitHub Desktop.
Sign up via omniauth with diffferent locales
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
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment