From dd69e0a0ea526aebb0fbd121ca9ef196b5ef0725 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 23 Aug 2023 18:48:50 +0000 Subject: [PATCH] Less abrupt link errors. --- src/ClientsRoutes.php | 40 ++++++++++++++++++++++++++++++------ templates/clients/index.twig | 5 +++++ 2 files changed, 39 insertions(+), 6 deletions(-) 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 @@