Files
MySMSAPio/app/controllers/admin/sessions_controller.rb
2026-07-28 03:04:27 +08:00

36 lines
982 B
Ruby

module Admin
class SessionsController < ActionController::Base
include AdminAuthentication
layout "admin"
# CSRF protection is enabled by default in ActionController::Base
# We need it for the create action but not for the new (GET) action
protect_from_forgery with: :exception
helper_method :current_admin, :logged_in?
def new
redirect_to admin_dashboard_path if current_admin
end
def create
admin = AdminUser.find_by(email: params[:email]&.downcase)
if admin&.authenticate(params[:password])
session[:admin_id] = admin.id
admin.update_last_login!
redirect_to admin_dashboard_path, notice: "Welcome back, #{admin.name}!"
else
flash.now[:alert] = "Invalid email or password"
render :new, status: :unprocessable_entity
end
end
def destroy
session.delete(:admin_id)
redirect_to admin_login_path, notice: "You have been logged out"
end
end
end