<?php

namespace App\Libraries\MyAuth;

use App\Entities\UserEntity;
use App\Libraries\CommonLibrary;

// 참고:https://github.com/SyntaxPhoenix/iloclient
abstract class MyAuth extends CommonLibrary
{
	private $_session = null;
	protected function __construct()
	{
		$this->_session = \Config\Services::session();
	}

	final public function login(UserEntity $entity): void
	{
		$this->_session->set(SESSION_NAMES['ISLOGIN'], true);
		$this->_session->set(SESSION_NAMES['AUTH'], [
			'uid' => $entity->getPK(),
			'name' => $entity->getTitle(),
			'role' => $entity->role
		]);
	}

	final public function logout(): void
	{
		// 세션 데이터 삭제
		$this->_session->remove(SESSION_NAMES['ISLOGIN']);
		$this->_session->remove(SESSION_NAMES['AUTH']);

		// 모든 세션 데이터 삭제
		$this->_session->destroy();

		// 세션 쿠키 삭제
		if (ini_get("session.use_cookies")) {
			$params = session_get_cookie_params();
			setcookie(
				session_name(),
				'',
				time() - 42000,
				$params["path"],
				$params["domain"],
				$params["secure"],
				$params["httponly"]
			);
		}

		// 세션 재생성
		session_start();
		$this->_session->regenerate(true);
	}
}
