Devise redirect back to the original location after sign in or sign up?

前端 未结 8 2245
野趣味
野趣味 2020-12-14 07:42

Here I\'m using Devise Gem for authentication. If someone want to open page without login then it redirect to sign_in page and after signed in it back to the page which user

8条回答
  •  北海茫月
    2020-12-14 08:27

    You don't need all this code. Devise (as the latest versions) already saves the location for you.

    Just use this:

    def after_sign_in_path_for(resource)
      request.env['omniauth.origin'] || stored_location_for(resource) || root_url
    end
    

    This will redirect user to the latest omniauth.source, or the stored_location and is last case, the root url.

    I thought I was required to create that method, but Devise already does it.

    Source: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in

提交回复
热议问题