tabletop-companion/app/controllers/sessions_controller.rb

34 lines
842 B
Ruby

# frozen_string_literal: true
class SessionsController < ApplicationController
skip_before_action :authenticate, only: [ :new, :create ]
def new
end
def create
Current.user = User.authenticate_by(
username: params[:username],
password: params[:password],
)
if Current.user && Current.user.verified?
session[:user_id] = Current.user.id
flash[:notice] = t(".success", name: Current.user.first_name)
redirect_to :root
elsif Current.user && !Current.user.verified?
flash[:alert] = t(".not_verified")
render :new, status: :unprocessable_entity
else
flash[:alert] = t(".error")
render :new, status: :unprocessable_entity
end
end
def destroy
reset_session
Current.user = nil
flash[:notice] = t(".success")
redirect_to login_path
end
end