68 lines
2.6 KiB
PHP
68 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Auth;
|
|
|
|
use App\Controllers\CommonController;
|
|
|
|
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 CommonController
|
|
{
|
|
public const PATH = 'auth';
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
$this->addActionPaths(self::PATH);
|
|
}
|
|
//로그인화면
|
|
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($this->getActionPaths(), $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', "로그아웃 중 오류가 발생했습니다.");
|
|
}
|
|
}
|
|
}
|