解決したいこと
webアプリでLINEログイン機能を実装しています。
LINEログイン画面まではアクセスできますが、「LINEログインに失敗しました」というメッセージが出てroot_pathにリダイレクトされます。
色々と調べたのですが、手詰まってしまったためご教授いただきたいです。
環境
Docker version 20.10.23
Rails 7.0.4
発生している問題・エラー
エラー文はないです。
該当するソースコード
・xxxxxxxxxxxxxxxxx_sorcery_external.rb ↓
class SorceryExternal < ActiveRecord::Migration[7.0] def change create_table :authentications do |t| t.integer :user_id, null: false t.string :provider, :uid, null: false t.timestamps null: false end add_index :authentications, [:provider, :uid] add_index :authentications, :user_id end end
・authentication.rb ↓
class Authentication < ApplicationRecord belongs_to :user end
・user.rb ↓
class User < ApplicationRecord authenticates_with_sorcery! has_many :menus, dependent: :destroy has_many :authentications, dependent: :destroy accepts_nested_attributes_for :authentications validates :name, presence: true, length: {maximum: 20} validates :email, presence: true, length: {maximum: 100}, uniqueness: true validates :password, length: { minimum: 3 }, confirmation: true, if: -> { new_record? || changes[:crypted_password] } validates :reset_password_token, uniqueness: true, allow_nil: true enum role: { general: 0, admin: 1 } end
・sorcery.rb ↓
config.line.key = Rails.application.credentials.dig(:line, :channel_id) config.line.secret = Rails.application.credentials.dig(:line, :channel_secret) config.line.callback_url = Settings.sorcery[:line_callback_url] config.line.scope = 'profile' config.line.bot_prompt = 'aggressive' config.line.user_info_mapping = { name: 'displayName', account_id: 'userId', email: 'userId' }
・development.yml ↓
sorcery: line_callback_url: 'https://xxxxxxxxxxxxxxxxxxxxxxxxx.ngrok-free.app/oauth/callback?provider=line'
・oauths_controller.rb ↓
class OauthsController < ApplicationController skip_before_action :require_login def oauth login_at(auth_params[:provider]) end def callback provider = auth_params[:provider] if @user = login_from(provider) redirect_to root_path, notice: "#{provider.titleize}でログインしました" else begin @user = create_from(provider) reset_session auto_login(@user) redirect_to root_path, notice: "#{provider.titleize}でログインしました" rescue StandardError redirect_to root_path, alert: "#{provider.titleize}でのログインに失敗しました" end end end private def auth_params params.permit(:code, :provider, :error, :state) end # Rails7で追加されたOpen Redirect protectionを無効化するためSorceryのメソッドをオーバーライド def login_at(provider_name, args = {}) redirect_to sorcery_login_url(provider_name, args), allow_other_host: true end end
・LINE DevelopersのLINEログイン設定にある、コールバックURLは、development.yml
に記述しているURLと同じです。
自分で試したこと
デバッグしてみました。
8: def callback 9: provider = auth_params[:provider] 10: if @user = login_from(provider) 11: redirect_to root_path, notice: "#{provider.titleize}でログインしました" 12: else 13: begin 14: @user = create_from(provider) 15: reset_session 16: auto_login(@user) 17: redirect_to root_path, notice: "#{provider.titleize}でログインしました" 18: rescue StandardError 19: binding.pry 20: => 21: redirect_to root_path, alert: "#{provider.titleize}でのログインに失敗しました" 22: end 23: end 24: end [1] pry(#<OauthsController>)> @user => nil [2] pry(#<OauthsController>)> provider => "line" [3] pry(#<OauthsController>)> auth_params => #<ActionController::Parameters {"code"=>"vfkoITZtZoGq7WxCiBQj", "provider"=>"line", "state"=>"2b743edc2c87199d4f811bfbe2a23f26"} permitted: true> [4] pry(#<OauthsController>)> login_from(provider) Authentication Load (0.8ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."uid" = $1 AND "authentications"."provider" = $2 ORDER BY "authentications"."id" ASC LIMIT $3 [["uid", "U391ba486e4e1bf49773ec743a2a68597"], ["provider", "line"], ["LIMIT", 1]] ↳ (pry):13:in `callback' => nil

回答1件
あなたの回答
tips
プレビュー