cookpad / omniauth-rails_csrf_protection

Provides CSRF protection on OmniAuth request endpoint on Rails application.
MIT License
230 stars 38 forks source link

Question: Redirecting to authorization path #11

Closed penguoir closed 3 years ago

penguoir commented 3 years ago

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?

penguoir commented 3 years ago

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