2023-08-15 13:50:39 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SessionsController < ApplicationController
|
2023-08-19 14:38:38 +00:00
|
|
|
before_action :set_user, only: [:create]
|
|
|
|
before_action :ensure_email_confirmed, only: [:create]
|
|
|
|
|
2023-08-15 13:50:39 +00:00
|
|
|
def new; end
|
|
|
|
|
|
|
|
def create
|
|
|
|
if @user.present? && @user.authenticate(params[:password])
|
|
|
|
session[:user_id] = @user.id
|
|
|
|
redirect_to @user, notice: t(".logged_in")
|
|
|
|
else
|
2023-08-18 19:55:12 +00:00
|
|
|
flash.now.alert = t(".login_fail")
|
2023-08-19 15:41:01 +00:00
|
|
|
render :new, status: :unprocessable_entity
|
2023-08-15 13:50:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_session
|
|
|
|
reset_session
|
|
|
|
redirect_to root_path, notice: t(".logged_out")
|
|
|
|
end
|
2023-08-19 14:38:38 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_user
|
|
|
|
@user = User.find_by(username: params[:username])
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_email_confirmed
|
|
|
|
return unless @user.requires_confirmation?
|
|
|
|
|
|
|
|
flash.alert = t(".account_not_confirmed")
|
|
|
|
redirect_to new_session_path
|
|
|
|
end
|
2023-08-15 13:50:39 +00:00
|
|
|
end
|