I am following the following tutorial:
And have just tried to add the 'remember me' functionality, so only up to around 4 minutes of the tutorial.
Everything seems fine but once I try to log in it just goes back to the home page and no one is logged in.
User.rb:
before_create { generate_token(:auth_token)
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
sessions_controller:
def create
user = User.find_by email: params[:email]
if user and user.authenticate params[:password]
# 'logging in' is performed by saving a user's id in the session variable
#session[:user_id] = user.id
#cookies.permanent[:auth_token] = user.auth_token
if params[:remember_me]
cookies.permanent[:auth_token] = user.auth_token
else
cookies[:auth_token] = user.auth_token
end
# redirect to last page or root_path (products/index.html.erb')
redirect_to root_path
else
flash.now.notice = "Invalid user/password"
render :new
end
end
def destroy
# the act of logging out is performed by simply setting the key (:user_id)
# in the session hash to a value of nil (nothing)
#session[:user_id] = nil
cookies.delete(:auth_token)
# redirect to root_path (products/index.html.erb')
redirect_to root_path
end
application_controller:
def authenticate_user
# if session[:user_id]
# @current_user = User.find session[:user_id]
# else
# @current_user = nil
# end
if cookies[:auth_token]
@current_user ||= User.find_by_auth_token!(cookies[:auth_token])
else
@current_user = nil
end
end
Can someone please tell me what the problem is.
Aucun commentaire:
Enregistrer un commentaire