LoginSignup
1
1

More than 5 years have passed since last update.

deviseでツイッターログイン

Last updated at Posted at 2019-02-06

この記事を元にdeviseを使ってツイッターでログインできるサイトを作る際、何個かエラーに遭遇したのでここに解決法を書いときます。
Railsのバージョンは5.1.6です。

Sign in with Twitter をクリックするとUnauthorized 403 Forbidden

Twitter AppsのSettingで callback URLsに
http://127.0.0.1:3000/users/auth/twitter/callback
http://localhost:3000/users/auth/twitter/callback
を追加
詳細

Sign in with Twitterの後 undefined method `current_sign_in_at

db/migrate/[time]_devise_create_users.rb
#...
## Trackable 以下のコメントを外す
t.integer  :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string   :current_sign_in_ip
t.string   :last_sign_in_ip
#...

キャッシュでエラー

app/controllers/users/omniauth_callbacks_controller.rb
  def callback_from(provider)
    provider = provider.to_s

    @user = User.find_for_oauth(request.env['omniauth.auth'])

    if @user.persisted?
      flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: provider.capitalize)
      sign_in_and_redirect @user, event: :authentication
    else
      # 最後に .except("extra") を付ける
      session["devise.#{provider}_data"] = request.env['omniauth.auth'].except("extra")
      redirect_to new_user_registration_url
    end
  end
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1