質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
LINE Messaging API

LINE Messaging APIは、メッセージの送信・返信ができるAPIです。Web APIを経由しアプリケーションサーバとLINEのAPIでやり取りが可能。複数のメッセージタイプや分かりやすいAPIリファレンスを持ち、グループチャットにも対応しています。

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

Q&A

解決済

1回答

856閲覧

LINEログインができない

退会済みユーザー

退会済みユーザー

総合スコア0

LINE Messaging API

LINE Messaging APIは、メッセージの送信・返信ができるAPIです。Web APIを経由しアプリケーションサーバとLINEのAPIでやり取りが可能。複数のメッセージタイプや分かりやすいAPIリファレンスを持ち、グループチャットにも対応しています。

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

0グッド

0クリップ

投稿2023/04/09 09:07

編集2023/04/10 02:40

解決したいこと

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

userテーブルにaccount_idカラムがないことにより、create_formメソッドが弾かれていました。
なので、account_idカラムを追加すると解決しました。

投稿2023/04/10 02:46

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問