LoadError in OmniauthCallbacksController#passthru (with devise/stripe connect)

冷暖自知 提交于 2019-12-12 01:47:41

问题


Trying to implement Stripe Connect, and am getting the following error when I click the "connect to stripe" button.

The action 'passthru' could not be found for OmniauthCallbacksController

users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
    def stripe_connect
        @user = current_user
        if @user.update_attributes({
          provider: request.env["omniauth.auth"].provider,
          uid: request.env["omniauth.auth"].uid,
          access_code: request.env["omniauth.auth"].credentials.token,
          publishable_key: request.env["omniauth.auth"].info.stripe_publishable_key
        })
          # anything else you need to do in response..
          sign_in_and_redirect @user, :event => :authentication
          set_flash_message(:notice, :success, :kind => "Stripe") if is_navigational_format?
        else
          session["devise.stripe_connect_data"] = request.env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
      end
end

models/user.rb

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:stripe_connect]

routes.rb

devise_for :users, controllers: { registrations: 'users/registrations', :omniauth_callbacks => "users/omniauth_callbacks" }

gemfile.rb

gem 'omniauth-stripe-connect'

initializers/stripe.rb

Rails.configuration.stripe = {
  :publishable_key => ENV['PUBLISHABLE_KEY'],
  :secret_key      => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]

initializers/devise.rb

config.omniauth :stripe_connect,
  ENV['STRIPE_CONNECT_CLIENT_ID'],
  ENV['STRIPE_SECRET_KEY'],
  :scope => 'read_write',
  :stripe_landing => 'register'

button link:

<%= link_to image_tag('blue-on-light.png'), user_stripe_connect_omniauth_authorize_path(:stripe_connect) %>

As I understand it with my noob Ruby mind, I need to define 'passthru'? how do I define it though? when I enter:

def passthru
end

the link doesn't work / the page reloads itself. Haven't been able to find a solution on here. What am I missing?

EDIT:

Changed my connect to stripe link to:

 <%= link_to image_tag('blue-on-light.png'), "/users/auth/stripe_connect" %>

The link takes me to the connect to stripe page, but when I click the "connect to stripe" button, the page cant be found, and doesn't load or redirect.


回答1:


Can you try changing

# app/controllers/omniauth_callbacks_controller.rb
class OmniauthCallbacksController < ApplicationController
    def stripe_connect
    ....

to

class OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def stripe_connect
      @user = User.find_for_stripe_connect(request.env['omniauth.auth'], current_user)
      set_notice_and_redirect
    end

    private

    def set_notice_and_redirect          
      if @user.persisted?
          flash[:notice] = 'Successfully signed in'
          set_flash_message(:notice, :success, :kind => "Stripe") if is_navigational_format?
        else
          session["devise.stripe_connect_data"] = request.env["omniauth.auth"]
          redirect_to new_user_registration_url
      end
    end
end

and in your user model

# Checks if user exists, otherwise create it
def self.find_for_stripe_connect(access_token, _ = nil)
  data = access_token.info
  user = User.where(email: data['email']).first_or_create(
    email: data['email'],
    password: Devise.friendly_token[0, 20],
    provider: request.env["omniauth.auth"].provider,
    uid: request.env["omniauth.auth"].uid,
    access_code: request.env["omniauth.auth"].credentials.token,
    publishable_key: request.env["omniauth.auth"].info.stripe_publishable_key
  )
  user
end

and also sing in path

<%= link_to image_tag('blue-on-light.png'), user_stripe_connect_omniauth_authorize %>

I think you don't need to define a passthru action. If you see the below two in the routes it can work. Authorize path is for redirecting user to stripe and callback is for redirecting user from stripe back to your site

$ rake routes

user_stripe_connect_omniauth_authorize /auth/stripe_connect(.:format)  ....
user_stripe_connect_omniauth_callback /auth/stripe_connect/callback(.:format) ....


来源:https://stackoverflow.com/questions/42045316/loaderror-in-omniauthcallbackscontrollerpassthru-with-devise-stripe-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!