# Authentication (restful, etc)

**URL:** <https://www.ruby-forum.com/t/authentication-restful-etc/135875>\
**Category:** Rails\
**Created:** [April 25, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/authentication-restful-etc/135875 "2008-04-25T20:54:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 25, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/authentication-restful-etc/135875/1 "2008-04-25T20:54:09Z")

</div>

I feel kind of “slow” asking this post - but I can’t get my head  
wrapped around the connection between the authentication of a user and  
the usage of the current\_user values in other controllers.

If I had a controller “my\_stuff” and I wanted two methods to require  
login, via restful, how can I go about doing that? Once I’ve required  
login, how can I go about accessing the session information, like  
userid and such?

I’ve tried, in the controller doing something like

@user = current\_user  
or  
@user = session.current\_user

or even something similar to @user = User.find\_by\_id(session.user\_id)

etc.

Just can’t get over / through “that” hurdle… once I do that, I’ll be  
happy with an auth tie-in to my app.

I come from a Perl world, and I know I recently mentioned that RoR is  
the best thing since sliced bread (read: mod\_perl) - I’m not fully  
able to get past authentication, etc.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 25, 2008, 9:16pm UTC](https://www.ruby-forum.com/t/authentication-restful-etc/135875/2 "2008-04-25T21:16:30Z")

</div>

I’m guessing that you’re you using the Restful Authentication plugin.

If not, have a look at it here:  
[http://agilewebdevelopment.com/plugins/restful\_authentication](http://agilewebdevelopment.com/plugins/restful_authentication)

After installing, examine the file RAILS\_ROOT/lib/  
authenticated\_system.rb, and you’ll see exactly how the current\_user  
method works. The short answer to your question is that you don’t  
need to do anything like “@user = User.find\_by\_id(session.user\_id).”

RestfulAuth also provides a method called ‘login\_required’ which you  
can use to require login for any controller method.

At the top of your controller,

before\_filer :login\_required, :except =\> :show

will require login on all methods except for show.

Hope this helps,

kb

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 25, 2008, 9:20pm UTC](https://www.ruby-forum.com/t/authentication-restful-etc/135875/3 "2008-04-25T21:20:38Z")

</div>

That helped a lot. Thank you! 🙂

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 25, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/authentication-restful-etc/135875/4 "2008-04-25T22:16:02Z")

</div>

In the case of restful\_authentication (and others like it), you add  
the functionality into ApplicationController by including  
AuthenticatedSystem. Typically your other controllers extend  
ApplicationController so they inherit the functionality.
