Authenticate user by twitter with devise and omniauth
We can authenticate an user with twitter.
Set route as below. This activates callback url from twitter.
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } devise_scope :user do get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session end
Then we need to implement registration sequence.
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def twitter @user = User.from_omniauth(request.env["omniauth.auth"]) if @user.persisted? sign_in_and_redirect @user, event: :authentication set_flash_message(:notice, :success, kind: "Twitter") if is_navigational_format? else @user.save! redirect_to root_path end end def failure redirect_to root_path end end