How to get authenticate to work?

28 views
Skip to first unread message

Charlie Lor

unread,
Jul 26, 2016, 8:19:36 AM7/26/16
to rubyonra...@googlegroups.com
In my SessionController, I tried to authenticate the user but it's
failing to authenticate even though I output the information and it's
all correct.I'm following this tutorial:
https://youtu.be/Hw6WtWJwRtU?list=PLS-X3A6Dr2qPXTSxMf6XPiO9HCkJLe51z

Below is my code:

def create
user = User.find_by(email: params[:email].downcase)
if user && user.authenticate(params[:password])
session[:user_id] = user.id
flash[:notice] = "Welcome back, #{user.name}!"
redirect_to user
else
flash.now[:alert] = "Invalid email/password combination!
#{user.name}, #{user.id}, #{params[:password]}"
render :new
end
end
def destroy
session[:user_id] = nil
redirect_to :new
end

The authenticate works fine in rails console but not when the rails
server. The problem is with this line
user.authenticate(params[:password]) of code.

Any help is much appreciated.

--
Posted via http://www.ruby-forum.com/.

macs...@gmail.com

unread,
Jul 27, 2016, 10:08:31 AM7/27/16
to Ruby on Rails: Talk
The password is in the session cookie:

if user && user.authenticate(params[:session][:password])
Reply all
Reply to author
Forward
0 new messages