# Login redirects that maintain parameter data

**URL:** <https://www.ruby-forum.com/t/login-redirects-that-maintain-parameter-data/157362>\
**Category:** Rails\
**Created:** [January 9, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/login-redirects-that-maintain-parameter-data/157362 "2009-01-09T23:24:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [January 9, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/login-redirects-that-maintain-parameter-data/157362/1 "2009-01-09T23:24:16Z")

</div>

Hey,  
I need some advice on something. I have a fairly large and complex  
website ([uberpwner.com](http://uberpwner.com)) and all authentication I do is done by before  
filters. Basically before\_filter :login\_required.  
login\_required does basic authentication and then redirects to the  
root\_path. Here is the essentials from the login system code (which  
is mostly taken from the beast forum):

```
 def login_required
  login_by_token unless logged_in?
  login_by_basic_auth unless logged_in?
  respond_to do |format|
    format.html { redirect_to login_path }
    format.js { render(:update) { |p| p.redirect_to

```

login\_path } }  
format.xml do  
headers[“WWW-Authenticate”] = %(Basic realm=“Beast”)  
render :text =\> “HTTP Basic: Access denied.\n”, :status  
=\> :unauthorized  
end  
end unless logged\_in? && authorized?  
end

def login\_by\_token  
self.current\_user = User.find\_by\_id\_and\_login\_key(\*cookies  
[:login\_token].split(“;”)) if cookies[:login\_token] and not logged\_in?  
end

```
  @@http_auth_headers = %w(X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION

```

Authorization)  
def login\_by\_basic\_auth  
auth\_key = @@http\_auth\_headers.detect { |h| request.env.has\_key?  
(h) }  
auth\_data = request.env[auth\_key].to\_s.split unless  
auth\_key.blank?  
self.current\_user = User.authenticate \*Base64.decode64(auth\_data  
[1]).split(‘:’)[0…1] if auth\_data && auth\_data[0] == ‘Basic’  
end

Now here is what I want: when a user clicks a page that requires  
authentication they should be redirected to the login page, then on  
successful login to the page they are going to. If they are trying to  
submit something to the site they should be redirected to login, then  
their submission should go through on successful login.

Any advice on how to achieve this would be very appreciated!  
JB

---

<div class="post-metadata">

**Author:** ![Jables](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58956e/32.png) [@Jables](https://www.ruby-forum.com/u/Jables)\
**Post date:** [January 10, 2009, 3:45am UTC](https://www.ruby-forum.com/t/login-redirects-that-maintain-parameter-data/157362/2 "2009-01-10T03:45:54Z")

</div>

I would suggest taking a look at Ryan B. screencast about restful  
authentication ([#67 restful\_authentication - RailsCasts](http://railscasts.com/episodes/67)), write a simple  
application or two using restful authentication, and then walk through  
the  
code to see how it all works. IIRC, it has support for doing just what  
you  
asked.

–wpd
