cookpad / omniauth-rails_csrf_protection

Provides CSRF protection on OmniAuth request endpoint on Rails application.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: Redirecting to authorization path

penguoir opened this issue · comments

I have a button that links to a controller like so:

# button in the view
button_to "Mark as complete", mark_complete_path

# controller
def mark_complete
  # mark as complete...
end

I'd like the controller to redirect the user to the auth path under certain conditions. Something like the following:

def mark_complete
  unless signed_in?
    redirect_to "/auth/github"
  end
end

But, because only POSTs are allowed against the auth path, and HTTP redirects don't allow POSTs, this doesn't work. Instead, it shows a No route matches [GET] "/auth/github".

Is there a way to authorize users through the controller?

Of course, as soon as I posted this issue, I found a solution.

  1. Install the repost gem.
  2. Use this code to redirect to the auth URL with POST:
if !current_user
  repost '/auth/github', options: { authenticity_token: :auto }
end