dbms/app/Controllers/Auth/AuthController.php
2025-06-05 19:07:27 +09:00

91 lines
2.9 KiB
PHP

<?php
namespace App\Controllers\Auth;
use App\Controllers\CommonController;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Entities\UserEntity;
use App\Helpers\AuthHelper;
abstract class AuthController extends CommonController
{
private $_helper = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->layout = "auth";
$this->uri_path = "auth/";
$this->view_path = "auth" . DIRECTORY_SEPARATOR;
$this->title = "사용자인증";
$this->individualStylesheets = [];
$this->individualScripts = [];
}
abstract protected function getSNSButton(): string;
abstract protected function login_process(string $action): UserEntity;
final public function getHelper(): mixed
{
if (!$this->_helper) {
$this->_helper = new AuthHelper($this->request);
}
return $this->_helper;
}
final public function getFields(): array
{
return ['id', 'passwd'];
}
//로그인화면
final public function login_form(): RedirectResponse|string
{
$action = __FUNCTION__;
try {
helper(['form']);
$this->sns_buttoh = $this->getSNSButton();
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
return $this->getResultPageByActon($action);
} catch (\Exception $e) {
if (env('app.debug.' . $action)) {
echo $e->getMessage();
exit;
} else {
return redirect()->back()->withInput()->with('error', $e->getMessage());
}
}
}
//로그인
final public function login(): RedirectResponse|string
{
$action = __FUNCTION__;
try {
$this->entity = $this->login_process($action);
return $this->getResultPageByActon($action, MESSAGES['LOGIN']);
} catch (\Exception $e) {
if (env('app.debug.' . $action)) {
echo $e->getMessage();
exit;
} else {
return redirect()->back()->withInput()->with('error', $e->getMessage());
}
}
}
//로그아웃
final public function logout(): RedirectResponse
{
try {
// $this->init(__FUNCTION__);
$this->getService()->logout();
// 홈페이지로 리다이렉트
return redirect()->route('/')->with('error', MESSAGES['LOGOUT']);
} catch (\Exception $e) {
log_message("error", $e->getMessage());
return redirect()->back()->with('error', "로그아웃 중 오류가 발생했습니다.");
}
}
}