@yujinyoutai

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

【Rails】『The change you wanted was rejected.』というエラー

解決したいこと

LINEログインの際に出現している『The change you wanted was rejected.』というエラーを解消したいです。

発生している問題・エラー

Railsを使ってwebアプリケーションを作っています。エラーが発生した実行環境はRubyのバージョン2.6.5で、Railsは6.0.5.1です。
herokuでデプロイを行っていて本番環境で発生しています。
deviseでのログイン機能も導入しています
自身のLINEアカウントではログインができたのですが、他の人に試してもらった所エラーが確認できました。
初学者で、理解が不足している為何を載せたらよいかわからず、関係のない場所でしたらすみません。

リンクをクリックするとユーザーのLINEアカウントが表示された画面まではいくのですが、ログインボタンを押した後下記のエラーが発生します
image.png

関係がありそうなgemはこのような感じです

gem 'omniauth', '~> 1.9.1'
gem 'omniauth-line'
gem 'omniauth-rails_csrf_protection'
gem 'line-bot-api'

linebot_controller.rb

class LinebotController < ApplicationController
  require 'line/bot' 
 
  protect_from_forgery :except => [:callback]
 
  def client
    @client ||= Line::Bot::Client.new { |config|
      config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
      config.channel_token = ENV["LINE_CHANNEL_TOKEN"]
    }
  end
 
  def callback
    body = request.body.read

    signature = request.env['HTTP_X_LINE_SIGNATURE']
    unless client.validate_signature(body, signature)
      error 400 do 'Bad Request' end
    end

    events = client.parse_events_from(body)

    events.each do |event|
      case event
      when Line::Bot::Event::Message
        case event.type
          when Line::Bot::Event::MessageType::Text
            message = {
              type: 'text',
              text: event.message['text']
            }
          client.reply_message(event['replyToken'], message)
          when Line::Bot::Event::MessageType::Follow #友達登録イベント
            userId = event['source']['userId'] 
            User.find_or_create_by(uid: userId)
          when Line::Bot::Event::MessageType::Unfollow#友達削除イベント
            userId = event['source']['userId']  
            user = User.find_by(uid: userId)
            user.destroy if user.present?
        end
      end
    end

    head :ok
  end
end

omniauth_callbacks_controller.rb

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  
  def line; basic_action
  end

  private
  def basic_action
    @omniauth = request.env["omniauth.auth"]
    if @omniauth.present?
      @profile = User.find_or_initialize_by(provider: @omniauth["provider"], uid: @omniauth["uid"])
      if @profile.email.blank?
        email = @omniauth["info"]["email"] ? @omniauth["info"]["email"] : "#{@omniauth["uid"]}-#{@omniauth["provider"]}@example.com"
        @profile = current_user || User.create!(provider: @omniauth["provider"], uid: @omniauth["uid"], email: email, name: @omniauth["info"]["name"], password: Devise.friendly_token[0, 20])
      end
      @profile.set_values(@omniauth)
      sign_in(:user, @profile)
    end
    flash[:notice] = "ログインしました"
    redirect_to root_path
  end

  def fake_email(uid, provider)
    "#{auth.uid}-#{auth.provider}@example.com"
  end
end

routes.rb

Rails.application.routes.draw do

  post '/callback' => 'linebot#callback'
  
  devise_for :users, controllers: {
    omniauth_callbacks: 'omniauth_callbacks'
  }
  root to: 'contents#index'
  resources :contents
  resources :users, only: :show

  devise_scope :user do
    get '/users', to: 'devise/registrations#new'
  end
end

herokuのログです
image.png

参考

0 likes

No Answers yet.

Your answer might help someone💌