Question: Redirecting to authorization path
penguoir opened this issue · 1 comments
penguoir commented
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 POST
s are allowed against the auth path, and HTTP redirects don't allow POST
s, 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
Of course, as soon as I posted this issue, I found a solution.
- Install the
repost
gem. - Use this code to redirect to the auth URL with POST:
if !current_user
repost '/auth/github', options: { authenticity_token: :auto }
end