简体   繁体   English

在 OmniAuth 中的某些条件下阻止用户访问 /auth/:provider

[英]Prevent user from accessing /auth/:provider under some conditions in OmniAuth

Is there any way to prevent users from accessing /auth/:provider if, say, they are not logged in?有没有办法阻止用户访问/auth/:provider如果他们没有登录? I've tried to use before_request_phase callback and Rack::Response to redirect them to sign in page but it haven't worked.我尝试使用before_request_phase回调和Rack::Response将它们重定向到登录页面,但没有奏效。

My application is not using omniauth for user authentication.我的应用程序没有使用 omniauth 进行用户身份验证。 Instead, it is used to connect third-party accounts to the user profile.相反,它用于将第三方帐户连接到用户配置文件。

Thanks!谢谢!

OmniAuth.config.before_request_phase = lambda do |env|
  user = env['warden'].authenticate!
end

Ok, the solution I found was to create a new OmniAuth Strategy which inherits from the one I wanted to use and to override the request_phase method.好的,我找到的解决方案是创建一个新的 OmniAuth 策略,它继承自我想要使用的策略并覆盖request_phase方法。 Could not get the same behaviour using only OmniAuth configs in its initializer.在其初始值设定项中仅使用 OmniAuth 配置无法获得相同的行为。

def request_phase
  if env['rack.session']['warden.user.user.key'].present?
    super
  else
    redirect '/'
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 omniauth 的路由从 /auth/:provider 更改为 /myapp/auth/:provider - How to change route of omniauth from /auth/:provider to /myapp/auth/:provider 如何在没有重定向的情况下从OmniAuth提供程序获取auth URL - How to get the auth URL from an OmniAuth provider without redirect 如何通过omniauth更改/ auth /:provider路径 - How to change /auth/:provider path by omniauth 为什么 Omniauth 重定向到 /auth/failure 而不是 /auth/:provider/failure? - Why does Omniauth redirect to /auth/failure and not /auth/:provider/failure? 如果不是当前用户,则阻止用户访问页面 - Prevent user from accessing page if not current user Omniauth 没有捕获初始获取“/auth/:provider”请求 - Omniauth isn't catching the initial get “/auth/:provider” request 在访问 Omniauth 路由之前验证设计用户 - Authenticate Devise User Before Accessing Omniauth Routes 如何从request.env [“omniauth.auth”](Rails)获取twitter用户名 - How to get twitter user name from request.env[“omniauth.auth”] (Rails) 使用 devise、omniauth 和 devise-token-auth 时,没有路由匹配“omniauth/:provider” - No route matches "omniauth/:provider" when using devise, omniauth and devise-token-auth OmniAuth的来源-从哪里调用/ auth / facebook - Origin of OmniAuth - Where was /auth/facebook called from
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM