Created
February 10, 2011 02:28
-
-
Save dira/819806 to your computer and use it in GitHub Desktop.
A Rake middleware to specify to OmniAuth where to redirect after successful authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/application.rb | |
config.middleware.use 'StoreRedirectTo' | |
# and make sure you autoload the contents of /lib | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/store_redirect_to.rb | |
class StoreRedirectTo | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
request = Rack::Request.new(env) | |
path = request.path | |
signin = path.match(%r(^/auth/[^/]+$)) && path != '/auth/failure' | |
signout = path == '/users/signout' | |
if signin || signout | |
redirect = request.params['redirect_to'] || request.referer | |
request.session['redirect_to'] = redirect if redirect | |
end | |
@app.call(env) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# after successful sign-in or sign-out | |
redirect_to session[:redirect_to] || root_path | |
session[:redirect_to] = nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment