diff --git a/login.php b/login.php index b5d6a88e..c5e71a61 100644 --- a/login.php +++ b/login.php @@ -51,44 +51,29 @@ $Search['error'] = 1; } } else if (!empty($_COOKIE[$sessionCookieKey])) { - $Search['mode'] = 2; - - $verificationResult = Session\Utils\Cookie\verifySessionCookie([ - 'userEntityFetcher' => function ($fetcherParams) { - $userId = $fetcherParams['userId']; - - $Query_GetUser = ''; - $Query_GetUser .= "SELECT `id`, `username`, `password`, `isAI` "; - $Query_GetUser .= "FROM {{table}} "; - $Query_GetUser .= "WHERE `id` = {$userId} LIMIT 1;"; - - return doquery($Query_GetUser, 'users'); - }, - ]); - - if (!$verificationResult['isSuccess']) { - switch ($verificationResult['error']['code']) { - case 'INVALID_USER_ID': - $Search['error'] = 2; - break; - case 'USER_NOT_FOUND': - $Search['error'] = 3; - break; - case 'INVALID_PASSWORD': - $Search['error'] = 4; - break; - } + $loginAttemptResult = Session\Input\CookieLogin\handleCookieLogin([]); - setcookie($sessionCookieKey, false, 0, '/', ''); - } else { - include_once($_EnginePath . '/includes/functions/IPandUA_Logger.php'); + if ($loginAttemptResult['isSuccess']) { + Session\Utils\Redirects\redirectToOverview(); - $UserData = $verificationResult['payload']['userEntity']; + die(); + } - IPandUA_Logger($UserData); + $Search['mode'] = 2; - header("Location: ./overview.php"); - die(); + switch ($loginAttemptResult['error']['code']) { + case 'NO_COOKIE': + $Search['error'] = 2; + break; + case 'INVALID_USER_ID': + $Search['error'] = 2; + break; + case 'USER_NOT_FOUND': + $Search['error'] = 3; + break; + case 'INVALID_PASSWORD': + $Search['error'] = 4; + break; } } diff --git a/modules/session/_includes.php b/modules/session/_includes.php index bd40cdc7..54fd5cd2 100644 --- a/modules/session/_includes.php +++ b/modules/session/_includes.php @@ -6,11 +6,14 @@ $includePath = $_EnginePath . 'modules/session/'; + include($includePath . './input/cookieLogin.inputHandler.php'); + include($includePath . './screens/LoginView/LoginView.component.php'); include($includePath . './screens/LoginView/components/LoginForm/LoginForm.component.php'); include($includePath . './utils/cookie.utils.php'); include($includePath . './utils/rateLimiter.utils.php'); + include($includePath . './utils/redirects.utils.php'); }); diff --git a/modules/session/input/cookieLogin.inputHandler.php b/modules/session/input/cookieLogin.inputHandler.php new file mode 100644 index 00000000..26a5eab2 --- /dev/null +++ b/modules/session/input/cookieLogin.inputHandler.php @@ -0,0 +1,65 @@ + true, + 'payload' => $payload, + ]; + }; + $createFailure = function ($error) { + return [ + 'isSuccess' => false, + 'error' => $error, + ]; + }; + + if (!(Session\Utils\Cookie\hasSessionCookie())) { + return $createFailure([ + 'code' => 'NO_COOKIE', + ]); + } + + $verificationResult = Session\Utils\Cookie\verifySessionCookie([ + 'userEntityFetcher' => function ($fetcherParams) { + $userId = $fetcherParams['userId']; + + $Query_GetUser = ''; + $Query_GetUser .= "SELECT `id`, `username`, `password`, `isAI` "; + $Query_GetUser .= "FROM {{table}} "; + $Query_GetUser .= "WHERE `id` = {$userId} LIMIT 1;"; + + return doquery($Query_GetUser, 'users'); + }, + ]); + + if (!$verificationResult['isSuccess']) { + $sessionCookieKey = getSessionCookieKey(); + + // TODO: Side effect, move elsewhere (?) + setcookie($sessionCookieKey, false, 0, '/', ''); + + return $createFailure([ + 'code' => $verificationResult['error']['code'], + ]); + } + + include_once($_EnginePath . '/includes/functions/IPandUA_Logger.php'); + + $UserData = $verificationResult['payload']['userEntity']; + + IPandUA_Logger($UserData); + + return $createSuccess([]); +} + +?> diff --git a/modules/session/input/index.php b/modules/session/input/index.php new file mode 100644 index 00000000..bc99142d --- /dev/null +++ b/modules/session/input/index.php @@ -0,0 +1,5 @@ + diff --git a/modules/session/utils/redirects.utils.php b/modules/session/utils/redirects.utils.php new file mode 100644 index 00000000..d25888ae --- /dev/null +++ b/modules/session/utils/redirects.utils.php @@ -0,0 +1,9 @@ +