RESTful authentication: an attempt is made to simply redirect to cookie-based authentication

I have a RoR application that uses a RESTful Authentication plugin. Everything works great. I recently turned on cookie based authentication, and that works fine too. The problem is that I want to change the default landing page when a user authenticates with a cookie. I want the authenticated user to be redirected to the same page to which they were redirected after a successful login. They are always directed to the source URL of the request. I am puzzling over this since I thought I understood how it works, and every change I make seems to have no effect.

I suspect this is something simple, but I obviously missed it. I would appreciate any feedback, recommendations or suggestions that you could offer.

+3
source share
5 answers

I solved the problem, but it is a little ugly in my opinion. Here is what I did.

In the cookie validation method, I set a session variable indicating the method of entering the cookie.

def login_from_cookie
  user = cookies[:auth_token] && User.find_by_remember_token(cookies[:auth_token])
  if user && user.remember_token?
    session[:cookie_login] = true   **# this is my addition**
    self.current_user = user
    handle_remember_cookie! false # freshen cookie token (keeping date)
    self.current_user
  end
end

Then in: before_filter set_current_userI just check this variable and redirect it if set to set the variable to zero.

def set_current_user
  Authorization.current_user = current_user
  if session[:cookie_login] 
    redirect_to :controller => :users, :action => :search
    session[:cookie_login] = false
  end
end

It is not very, but it really works. I am definitely open to any suggestions on how to clean this.

+1
source

:

redirect_to :controller => 'dashboard', :action => 'index'
0

Bort, , , Restful_Authentication, successful_login, restful_auth:

redirect_back_or_default( root_path )

authenticated_system.rb

    def redirect_back_or_default(default)
      redirect_to(session[:return_to] || default)
      session[:return_to] = nil
    end
0

,

map.root :controller => :users, :action => :search

before_filter, , - " "? , , cookie, . , cookie auth, . , .

0

Restful Authentication URL-, , . , , , cookie, .

, , authenticated_system.rb

 def login_from_cookie
  user = cookies[:auth_token] && User.find_by_remember_token(cookies[:auth_token])
  if user && user.remember_token?
    self.current_user = user
    session[:return_to] = nil # This clears out the return value so the user will get redirected to the default path
    handle_remember_cookie! false # freshen cookie token (keeping date)
    self.current_user
  end
end

[: return_to] = nil

, , . :

redirect_back_or_default(the_path_you_want_to_send_them_to)
0

Source: https://habr.com/ru/post/1708986/


All Articles