Skip to content

Commit 62bdf96

Browse files
add Delete after check condition to confirmation code feature
1 parent 41fb36e commit 62bdf96

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ TokensValidation is a PHP library designed to generate and verify authentication
2020
1. [By URL](#1--by-url)
2121
2. [By Typing](#2--by-typing)
2222
3. [WhatFor Field](#whatfor-field)
23-
4. [Single Token Per Period](#single-token-per-period)
23+
4. [Delete after check](#delete-after-check)
24+
5. [Single Token Per Period](#single-token-per-period)
2425
4. [Tokens Generator](#tokens-generator)
2526
5. [Token Expiration](#token-expiration)
2627
6. [Invitations](#invitations)
@@ -333,6 +334,20 @@ $result = TokensValidation::checkConfirmationCode(code: $token, whatFor: "email-
333334

334335
If the "whatFor" parameter does not match the intended purpose of the confirmation code, the validation process will fail.
335336

337+
#### Delete after check:
338+
In some cases, you may only want to check the token and keep it active like for examples (middleware checks)
339+
you want just to check the token if its valid, then check it later in another position.
340+
This parameter allows you to specify whether the token will be deleted after the validation succeeded or not.
341+
342+
```PHP
343+
$confirmationToken = TokensValidation::createNewConfirmationToken(
344+
userId: $uid,
345+
confirmationType: ConfirmationsTokenTypes::SMALL_CODE,
346+
whatFor: "email-confirmation",
347+
deleteAfterCheck: false, //true by default
348+
);
349+
```
350+
336351
#### Single Token Per Period:
337352
To avoid creating multiple confirmation code at the same moment (before expiration),
338353
you can set "**singleTokenPerTime**" parameter to true when calling the **createNewConfirmationToken** function.

src/TokensValidation.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,10 @@ public static function checkAuthTokenOrDie(string $fingerPrint = "", ?string $au
612612
* @param string $code
613613
* @param string|null $encryptedUserId
614614
* @param string $whatFor
615+
* @param bool $deleteAfterCheck
615616
* @return ConfirmationTokenResponse
616617
*/
617-
public static function checkConfirmationCode(string $code, string $encryptedUserId = null, string $whatFor = "default"): ConfirmationTokenResponse
618+
public static function checkConfirmationCode(string $code, string $encryptedUserId = null, string $whatFor = "default", bool $deleteAfterCheck = true): ConfirmationTokenResponse
618619
{
619620
$confirmationTokenResultsBuilder = ConfirmationTokenResponse::builder()
620621
->setValidationSucceed(false);
@@ -638,7 +639,9 @@ public static function checkConfirmationCode(string $code, string $encryptedUser
638639
if ($confirmationTokenModel->whatFor == $whatFor || $whatFor == "default") {
639640
$confirmationTokenResultsBuilder->setValidationSucceed(true);
640641
$confirmationTokenResultsBuilder->withWhatFor($whatFor);
641-
ConfirmationTokenModel::find($confirmationTokenModel->id)->delete();
642+
if ($deleteAfterCheck) {
643+
ConfirmationTokenModel::find($confirmationTokenModel->id)->delete();
644+
}
642645
return $confirmationTokenResultsBuilder->build();
643646
}
644647
else{
@@ -663,14 +666,15 @@ public static function checkConfirmationCode(string $code, string $encryptedUser
663666
/**
664667
* @param string $url
665668
* @param string $whatFor
669+
* @param bool $deleteAfterCheck
666670
* @return ConfirmationTokenResponse
667671
*/
668-
public static function checkConfirmationUrl(string $url, string $whatFor = "default"): ConfirmationTokenResponse
672+
public static function checkConfirmationUrl(string $url, string $whatFor = "default", bool $deleteAfterCheck = true): ConfirmationTokenResponse
669673
{
670674
/** @var UserIdAndToken $userIdAndToken */
671675
$userIdAndToken = call_user_func_array([new TokensValidation::$ConfirmationUrlBuilder(), 'getUserIdAndTokenFromUrl'], [$url]);
672676
if ($userIdAndToken != null) {
673-
return self::checkConfirmationCode($userIdAndToken->getToken(), $userIdAndToken->getUserId(), $whatFor);
677+
return self::checkConfirmationCode($userIdAndToken->getToken(), $userIdAndToken->getUserId(), $whatFor, $deleteAfterCheck);
674678
}
675679
return ConfirmationTokenResponse::builder()
676680
->setException(new Exception("can't get userIdAndToken"))
@@ -682,14 +686,15 @@ public static function checkConfirmationUrl(string $url, string $whatFor = "defa
682686
/**
683687
* @param array $_GET_ARRAY
684688
* @param string $whatFor
689+
* @param bool $deleteAfterCheck
685690
* @return ConfirmationTokenResponse
686691
*/
687-
public static function checkConfirmationUrlParamsFromGET(array $_GET_ARRAY, string $whatFor = "default"): ConfirmationTokenResponse
692+
public static function checkConfirmationUrlParamsFromGET(array $_GET_ARRAY, string $whatFor = "default", bool $deleteAfterCheck = true): ConfirmationTokenResponse
688693
{
689694
/** @var UserIdAndToken $userIdAndToken */
690695
$userIdAndToken = call_user_func_array([new TokensValidation::$ConfirmationUrlBuilder(), 'getUserIdAndTokenFromGET'], [$_GET_ARRAY]);
691696
if ($userIdAndToken != null) {
692-
return self::checkConfirmationCode($userIdAndToken->getToken(), $userIdAndToken->getUserId(), $whatFor);
697+
return self::checkConfirmationCode($userIdAndToken->getToken(), $userIdAndToken->getUserId(), $whatFor, $deleteAfterCheck);
693698
}
694699
return ConfirmationTokenResponse::builder()
695700
->setException(new Exception("can't get userIdAndToken"))

0 commit comments

Comments
 (0)