diff --git a/src/ClientsRoutes.php b/src/ClientsRoutes.php index 22b14db..a28af9b 100644 --- a/src/ClientsRoutes.php +++ b/src/ClientsRoutes.php @@ -40,7 +40,30 @@ class ClientsRoutes { } } - public function getClients() { + private const CLIENTS_ERRORS = [ + 'link' => [ + 'already' => 'You already have a linked Minecraft username, unlink the other one first.', + 'format' => 'The Link Code you entered was is not in the correct format, check your input!', + 'code' => 'The Link Code you entered is not valid, make sure you typed it correctly!', + ], + ]; + + public function getClients($response, $request) { + $errorCode = (string)$request->getParam('error'); + if($errorCode !== '') { + $errorCode = explode(':', $errorCode, 2); + if(count($errorCode) === 2 + && array_key_exists($errorCode[0], self::CLIENTS_ERRORS) + && array_key_exists($errorCode[1], self::CLIENTS_ERRORS[$errorCode[0]])) + $this->templating->addVars([ + 'error' => [ + 'section' => $errorCode[0], + 'code' => $errorCode[1], + 'message' => self::CLIENTS_ERRORS[$errorCode[0]][$errorCode[1]], + ], + ]); + } + try { $linkInfo = $this->accountLinks->getLink(userInfo: $this->authInfo->user_id); $clients = $this->authorisations->getAuthorisations($linkInfo); @@ -55,20 +78,25 @@ class ClientsRoutes { } public function postLink($response, $request) { - if($this->accountLinks->checkHasLink($this->authInfo->user_id)) - return 403; + if($this->accountLinks->checkHasLink($this->authInfo->user_id)) { + $response->redirect('/clients?error=link:already'); + return; + } $body = $request->getContent(); $code = (string)$body->getParam('code'); - if(strlen($code) !== 10) - return 400; + if(strlen($code) !== 10) { + $response->redirect('/clients?error=link:format'); + return; + } $code = strtr(strtoupper($code), '0189', 'OIBG'); try { $verifyInfo = $this->verifications->getVerification(code: $code); } catch(RuntimeException $ex) { - return 404; + $response->redirect('/clients?error=link:code'); + return; } $this->verifications->deleteVerification($verifyInfo); diff --git a/templates/clients/index.twig b/templates/clients/index.twig index 7591f51..786bddc 100644 --- a/templates/clients/index.twig +++ b/templates/clients/index.twig @@ -10,6 +10,11 @@
This will associate a Minecraft username with your Flashii ID. You may only have one linked at a time. In order to obtain a link code, connect to one of the Minecraft servers.
+ + {% if error is defined and error.section == 'link' %} +{{ error.message }}
+ {% endif %} +