认证失败时如何重定向到其他网址

I would like to redirect to another url when auth failed.
Any idea please thanks!

plugins
  myplugin
    lib
      auth
        my_auth_controller.rb
          def after_authenticate(auth_hash)

            if not auth
              redirect_to "https://example.com"
            end 

            result = Auth::Result.new
          end
  1. 在您的代码中,您有一个 if not auth 条件,但没有相应的 else 语句。
  2. redirect_to 方法应该在控制器操作中使用。如果您在 after_authenticate 等单独的方法中使用它,请确保它在控制器的范围内。
    因此,这是正确的代码。
def after_authenticate(auth_hash)
  if not auth
    redirect_to "https://example.com"
  else
    # 身份验证成功,继续进行身份验证过程
    result = Auth::Result.new
  end
end

结果是,如果身份验证失败 auth 不为 true,它将重定向到“https://example.com”。否则,如果身份验证成功,它将继续进行身份验证过程。
为了验证,您可以检查浏览器开发者工具以检查网络请求和重定向,或使用任何在线工具,例如redirect checker。这可以帮助您识别重定向问题和状态码。