Skip to content

Commit

Permalink
add Delete after check condition to confirmation code feature
Browse files Browse the repository at this point in the history
  • Loading branch information
HichemTab-tech committed Jul 29, 2023
1 parent 41fb36e commit 62bdf96
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 11 additions & 6 deletions src/TokensValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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{
Expand All @@ -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"))
Expand All @@ -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"))
Expand Down

0 comments on commit 62bdf96

Please sign in to comment.