Skip to content

Instantly share code, notes, and snippets.

@dira
Created February 10, 2011 02:28
Show Gist options
  • Save dira/819806 to your computer and use it in GitHub Desktop.
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
# config/application.rb
config.middleware.use 'StoreRedirectTo'
# and make sure you autoload the contents of /lib
# 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
# 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