105 lines
4.0 KiB
PHP
105 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Libraries\MySocket\GoogleSocket\API as GoogleSocket;
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use App\Services\UserService;
|
|
use App\Helpers\UserHelper;
|
|
use App\Services\Auth\GoogleService;
|
|
use App\Services\Auth\LocalService;
|
|
|
|
class UserController extends CommonController
|
|
{
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
$this->title = lang("{$this->getService()->getClassPath()}.title");;
|
|
$this->helper = new UserHelper();
|
|
}
|
|
protected function getService(): UserService
|
|
{
|
|
if ($this->service === null) {
|
|
$this->service = new UserService();
|
|
}
|
|
return $this->service;
|
|
}
|
|
protected function init(string $action, array $fields = []): void
|
|
{
|
|
$this->action = $action;
|
|
$this->fields = count($fields) ? $fields : ['id', 'passwd'];
|
|
}
|
|
//로그인화면
|
|
public function login_form(): RedirectResponse|string
|
|
{
|
|
try {
|
|
$this->init('login');
|
|
helper(['form']);
|
|
//구글 로그인 BUTTON용
|
|
$google_socket = new GoogleSocket();
|
|
$this->google_url = $google_socket->createAuthUrl();
|
|
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
|
|
return view(
|
|
$this->view_path . "login",
|
|
data: ['viewDatas' => $this->getViewDatas()]
|
|
);
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
|
|
}
|
|
}
|
|
//로그인처리
|
|
public function login(): RedirectResponse|string
|
|
{
|
|
try {
|
|
$this->init('login');
|
|
$this->formDatas = $this->create_validate($this->action, $this->fields);
|
|
$auth = new LocalService();
|
|
$auth->login($auth->checkUser($this->formDatas));
|
|
$this->message = "로그인 성공";
|
|
log_message("notice", __FUNCTION__ . $this->message);
|
|
// 이전 URL로 리다이렉트
|
|
return redirect()->to($this->myauth->popPreviousUrl())->with('message', $this->message);
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
|
|
}
|
|
}
|
|
public function google_login(): RedirectResponse|string
|
|
{
|
|
try {
|
|
$access_code = $this->request->getVar('code');
|
|
if (!$access_code) {
|
|
throw new \Exception("구글 로그인 실패");
|
|
}
|
|
$auth = new GoogleService(new GoogleSocket());
|
|
$auth->login($auth->checkUser($access_code));
|
|
$this->message = "로그인 성공";
|
|
log_message("notice", __FUNCTION__ . $this->message);
|
|
// 이전 URL로 리다이렉트
|
|
return redirect()->to($this->myauth->popPreviousUrl())->with('message', $this->message);
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
|
|
}
|
|
}
|
|
//로그아웃
|
|
public function logout(): RedirectResponse
|
|
{
|
|
try {
|
|
$auth = new LocalService();
|
|
$auth->logout();
|
|
// 성공 메시지 설정
|
|
$message = "로그아웃 되었습니다.";
|
|
// 홈페이지로 리다이렉트
|
|
return redirect()->route('/')->with('message', $message);
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
return redirect()->back()->with('error', "로그아웃 중 오류가 발생했습니다.");
|
|
}
|
|
}
|
|
}
|