diff --git a/README.md b/README.md index 0726fa9..29ce79b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ TokensValidation is a PHP library designed to generate and verify authentication 1. [By URL](#1--by-url) 2. [By Typing](#2--by-typing) 3. [WhatFor Field](#whatfor-field) - 4. [Single Token Per Period](#single-token-per-period) + 4. [Delete after check](#delete-after-check) + 5. [Single Token Per Period](#single-token-per-period) 4. [Tokens Generator](#tokens-generator) 5. [Token Expiration](#token-expiration) 6. [Invitations](#invitations) @@ -333,6 +334,20 @@ $result = TokensValidation::checkConfirmationCode(code: $token, whatFor: "email- If the "whatFor" parameter does not match the intended purpose of the confirmation code, the validation process will fail. +#### Delete after check: +In some cases, you may only want to check the token and keep it active like for examples (middleware checks) +you want just to check the token if its valid, then check it later in another position. +This parameter allows you to specify whether the token will be deleted after the validation succeeded or not. + +```PHP +$confirmationToken = TokensValidation::createNewConfirmationToken( + userId: $uid, + confirmationType: ConfirmationsTokenTypes::SMALL_CODE, + whatFor: "email-confirmation", + deleteAfterCheck: false, //true by default + ); +``` + #### Single Token Per Period: To avoid creating multiple confirmation code at the same moment (before expiration), you can set "**singleTokenPerTime**" parameter to true when calling the **createNewConfirmationToken** function. diff --git a/src/TokensValidation.php b/src/TokensValidation.php index 28b0fcb..b706e85 100644 --- a/src/TokensValidation.php +++ b/src/TokensValidation.php @@ -612,9 +612,10 @@ public static function checkAuthTokenOrDie(string $fingerPrint = "", ?string $au * @param string $code * @param string|null $encryptedUserId * @param string $whatFor + * @param bool $deleteAfterCheck * @return ConfirmationTokenResponse */ - public static function checkConfirmationCode(string $code, string $encryptedUserId = null, string $whatFor = "default"): ConfirmationTokenResponse + public static function checkConfirmationCode(string $code, string $encryptedUserId = null, string $whatFor = "default", bool $deleteAfterCheck = true): ConfirmationTokenResponse { $confirmationTokenResultsBuilder = ConfirmationTokenResponse::builder() ->setValidationSucceed(false); @@ -638,7 +639,9 @@ public static function checkConfirmationCode(string $code, string $encryptedUser if ($confirmationTokenModel->whatFor == $whatFor || $whatFor == "default") { $confirmationTokenResultsBuilder->setValidationSucceed(true); $confirmationTokenResultsBuilder->withWhatFor($whatFor); - ConfirmationTokenModel::find($confirmationTokenModel->id)->delete(); + if ($deleteAfterCheck) { + ConfirmationTokenModel::find($confirmationTokenModel->id)->delete(); + } return $confirmationTokenResultsBuilder->build(); } else{ @@ -663,14 +666,15 @@ public static function checkConfirmationCode(string $code, string $encryptedUser /** * @param string $url * @param string $whatFor + * @param bool $deleteAfterCheck * @return ConfirmationTokenResponse */ - public static function checkConfirmationUrl(string $url, string $whatFor = "default"): ConfirmationTokenResponse + public static function checkConfirmationUrl(string $url, string $whatFor = "default", bool $deleteAfterCheck = true): ConfirmationTokenResponse { /** @var UserIdAndToken $userIdAndToken */ $userIdAndToken = call_user_func_array([new TokensValidation::$ConfirmationUrlBuilder(), 'getUserIdAndTokenFromUrl'], [$url]); if ($userIdAndToken != null) { - return self::checkConfirmationCode($userIdAndToken->getToken(), $userIdAndToken->getUserId(), $whatFor); + return self::checkConfirmationCode($userIdAndToken->getToken(), $userIdAndToken->getUserId(), $whatFor, $deleteAfterCheck); } return ConfirmationTokenResponse::builder() ->setException(new Exception("can't get userIdAndToken")) @@ -682,14 +686,15 @@ public static function checkConfirmationUrl(string $url, string $whatFor = "defa /** * @param array $_GET_ARRAY * @param string $whatFor + * @param bool $deleteAfterCheck * @return ConfirmationTokenResponse */ - public static function checkConfirmationUrlParamsFromGET(array $_GET_ARRAY, string $whatFor = "default"): ConfirmationTokenResponse + public static function checkConfirmationUrlParamsFromGET(array $_GET_ARRAY, string $whatFor = "default", bool $deleteAfterCheck = true): ConfirmationTokenResponse { /** @var UserIdAndToken $userIdAndToken */ $userIdAndToken = call_user_func_array([new TokensValidation::$ConfirmationUrlBuilder(), 'getUserIdAndTokenFromGET'], [$_GET_ARRAY]); if ($userIdAndToken != null) { - return self::checkConfirmationCode($userIdAndToken->getToken(), $userIdAndToken->getUserId(), $whatFor); + return self::checkConfirmationCode($userIdAndToken->getToken(), $userIdAndToken->getUserId(), $whatFor, $deleteAfterCheck); } return ConfirmationTokenResponse::builder() ->setException(new Exception("can't get userIdAndToken"))