dbmsv4/app/Controllers/Auth/AuthController.php
2025-11-20 14:01:39 +09:00

80 lines
3.3 KiB
PHP

<?php
namespace App\Controllers\Auth;
use App\Controllers\AbstractWebController;
use App\Entities\UserEntity;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Exceptions\ValidationException;
use Psr\Log\LoggerInterface;
abstract class AuthController extends AbstractWebController
{
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->addActionPaths('auth');
}
final protected function getLayout(): string
{
return 'auth';
}
protected function action_init_process(string $action): void
{
parent::action_init_process($action);
$this->addViewDatas('layout', $this->getLayout());
$this->addViewDatas('helper', $this->service->getHelper());
$this->addViewDatas('formFields', $this->service->getFormService()->getFormFields());
$this->addViewDatas('formRules', $this->service->getFormService()->getFormRules());
$this->addViewDatas('formFilters', $this->service->getFormService()->getFormFilters());
$this->addViewDatas('formOptions', $this->service->getFormService()->getFormOptions());
}
//로그인화면
final public function login_form(): string|RedirectResponse
{
$action = __FUNCTION__;
try {
//초기화
$this->action_init_process($action);
} catch (\Exception $e) {
log_message('error', $e->getMessage());
session()->setFlashdata('message', $e->getMessage());
}
return $this->action_render_process($action, $this->getViewDatas());
}
//로그인처리
abstract protected function login_process(): UserEntity;
final public function login(): RedirectResponse
{
try {
$this->login_process();
$redirect_url = $this->getAuthContext()->popPreviousUrl() ?? implode(DIRECTORY_SEPARATOR, $this->getActionPaths());
return redirect()->to($redirect_url)->with('message', MESSAGES['LOGIN']);
} catch (ValidationException $e) {
// 검증 실패 시 폼으로 돌아가서 오류 메시지 표시
log_message('error', $e->getMessage());
return redirect()->back()->withInput()->with('message', $e->getMessage());
} catch (\Exception $e) {
log_message('error', $e->getMessage());
return redirect()->back()->withInput()->with('message', $e->getMessage());
// return redirect()->to($this->getMyAuth()->popPreviousUrl())->with('message', $e->getMessage());
}
}
//로그아웃
abstract protected function logout_process(): void;
final public function logout(): RedirectResponse
{
try {
$this->logout_process();
// 홈페이지로 리다이렉트
$redirect_url = $this->getAuthContext()->popPreviousUrl() ?? "/";
return redirect()->route($redirect_url)->with('message', MESSAGES['LOGOUT']);
} catch (\Exception $e) {
log_message("error", $e->getMessage());
return redirect()->back()->withInput()->with('message', "로그아웃 중 오류가 발생했습니다.");
}
}
}