RBOnRails-learning/app/controllers/sessions_controller.rb

24 lines
534 B
Ruby
Raw Permalink Normal View History

2024-08-08 09:19:44 +00:00
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:email].downcase)
if user && user.authenticate(params[:password])
session[:user_id] = user.id
flash[:notice] = "Logged in successfully."
redirect_to root_path
else
flash[:alert] = "Invalid email or password"
render :new
end
end
def destroy
session[:user_id] = nil
reset_session
flash[:notice] = "Logged out successfully."
redirect_to root_path
end
end