Authenticate static files with Devise?

两盒软妹~` 提交于 2021-02-11 13:20:53

问题


I have a static Jekyll support page on my site served in /public/support. The main rails app is behind devise - the whole thing. If you are not authenticated you get kicked back to the login. Can I 'hide' this static site behind the Devise authentication - i.e. only allow access to the static pages when authenticated?


回答1:


I ended up finding this:

https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-subrequest-authentication/

In my NGINX config I have this:

  location /support/ {
    auth_request /auth;
    auth_request_set $auth_status $upstream_status;
    error_page 403 https://$host;
  }

In my application controller I have:

before_action :authenticate_user!, except: :auth

This by-passes Devise.

In routes:

get '/auth', to: 'errors#auth'

It just made sense to add it to my existing custom errors controller.

Then in the controller:

  def auth
    user_signed_in? ? render(status: 200, layout: :blank) : render(status: 403, layout: :blank)
  end

The blank layout has no content - just a <%= yield %>.

If the user has an open Devise session they can access the support site otherwise they get redirected to the login page (default for Devise).



来源:https://stackoverflow.com/questions/64054247/authenticate-static-files-with-devise

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