# current user's session id?

**URL:** <https://discuss.rubyonrails.org/t/current-users-session-id/22256>\
**Category:** rubyonrails-talk\
**Created:** [December 26, 2007, 5:34pm UTC](https://discuss.rubyonrails.org/t/current-users-session-id/22256 "2007-12-26T17:34:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phillip\_Koebbe](https://avatars.discourse-cdn.com/v4/letter/p/d9b06d/32.png) [@Phillip\_Koebbe](https://discuss.rubyonrails.org/u/Phillip_Koebbe)\
**Post date:** [December 26, 2007, 5:34pm UTC](https://discuss.rubyonrails.org/t/current-users-session-id/22256/1 "2007-12-26T17:34:49Z")

</div>

To be a bit more clear, you can use whatever you want in the session, it doesn't have to be :id. It can be :user\_id, :logged\_in\_user\_id, whatever. But Patrick is correct in that the value has to get set when the user is logged in. If you're using something like acts\_as\_authenticated, it handles all of that for you and you can use

current\_user.id

But if you're rolling your own like I do, then you set the session variable when the user successfully authenticates. Something like:

# find user by email and password user = User.find\_by\_email\_and\_password(params[:email], params[:password]) if user &nbsp;&nbsp;session[:user\_id] = user.id &nbsp;&nbsp;redirect\_to :controller =\> 'user', :action =\> 'home' else &nbsp;&nbsp;flash[:error] = "Invalid login!" &nbsp;&nbsp;redirect\_to :action =\> 'login' end

Peace, Phillip
