PHP code example of hichemtab-tech / tokens-validation
1. Go to this page and download the library: Download hichemtab-tech/tokens-validation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hichemtab-tech / tokens-validation example snippets
use HichemtabTech\TokensValidation\TokensValidation;
:setDBNAME("test");
TokensValidation::setDBUSER("root");
TokensValidation::setDBPASS("");
TokensValidation::prepare();
$authToken = TokensValidation::createNewAuthToken(
userId: $uid,
usingCookies: true
);
// you can print $authToken for additional informations about the created token.
$result = TokensValidation::checkAuthToken();
if ($result->isValidationSucceed()) {
// log in the user automatically,
//for example :
autoLogin($result->getUserId());
}
else{
//throw an error or redirect to log in
}
$result = TokensValidation::checkAuthToken(authToken: $authToken);
if ($result->isValidationSucceed()) {
// log in the user automatically,
//for example :
echo $result->getUserId();
echo $result->getNewToken()->getContent();// save it in cookies or whatever you want
autoLogin($result->getUserId());
}
else{
//throw an error or redirect to log in
}
use HichemtabTech\TokensValidation\Actions\Authentication\AuthTokenCookiesHandler;
class MyAuthTokenCookiesHandler extends AuthTokenCookiesHandler
{
public function save(AuthToken $authToken): void
{
//save the $authToken in cookies or what ever you want
}
public function get(): ?string
{
//get it
}
public function delete(): void
{
//delete it
}
}
TokensValidation::$AuthTokenCookiesHandler = MyAuthTokenCookiesHandler::class;
// this should be called after preparation
$confirmationToken = TokensValidation::createNewConfirmationToken(
userId: $uid,
confirmationType: ConfirmationsTokenTypes::IN_URL
);
echo $confirmationToken->getContent();// if you want to view the long confirmation token
echo $confirmationToken->getUrl("http://localhost/email-check");// to get the full prepared url with the base url, http://localhost/email-check
//you will get the url like this:
//http://localhost/email-check?u=def5020080134ecc82ee1c1c2536ccdb0ec3c50161a9b6ab6f0f24c34730b73174327d2990ef8f583cec5f86ba7c3d44c5a5c0adae17313d09d5479fbe83c33e91f00d3902699507fc16266931be4e0f90382e4614aba6d8&c=nkyZDxMqbnS2oPs
$result = TokensValidation::checkConfirmationUrl(url: $url);
if ($result->isValidationSucceed()) {
//for example :
echo $result->getUserId();
//continue the request
}
else{
//throw an error
}
use HichemtabTech\TokensValidation\Actions\Confirmation\ConfirmationUrlBuilder;
use HichemtabTech\TokensValidation\Actions\Confirmation\UserIdAndToken;
use HichemtabTech\TokensValidation\Model\Confirmation\ConfirmationToken;
use Purl\Url;
class MyConfirmationUrlBuilder extends ConfirmationUrlBuilder
{
public function getUrl(ConfirmationToken $confirmationToken, string $baseUrl): string
{
$url = new Url($baseUrl);
$url->query->set("uid", $confirmationToken->getUserId());// for userId
$url->query->set("token", $confirmationToken->getContent());// for Code
return $url->getUrl();
}
public function getUserIdAndTokenFromUrl(string $url): UserIdAndToken
{
$url = new Url($url);
return UserIdAndToken::builder()
->setUserId($url->query->get("uid"))
->setToken($url->query->get("token"))
->build();
}
public function getUserIdAndTokenFromGET(array $_GET_ARRAY): UserIdAndToken
{
return UserIdAndToken::builder()
->setUserId($_GET_ARRAY["uid"]??"")
->setToken($_GET_ARRAY["token"]??"")
->build();
}
}
TokensValidation::$ConfirmationUrlBuilder = MyConfirmationUrlBuilder::class;
// this should be called after preparation
$confirmationToken = TokensValidation::createNewConfirmationToken(
userId: $uid,
confirmationType: ConfirmationsTokenTypes::SMALL_CODE
);
echo $confirmationToken->getContent();
$result = TokensValidation::checkConfirmationCode(code: $token);
if ($result->isValidationSucceed()) {
//for example :
echo $result->getUserId();
//continue the request
}
else{
//throw an error
}
// Set authentication token expiration period to 2 days
TokensValidation::setAuthTokenExpirationDelay(2 * 24 * 60 * 60); // seconds
// Set confirmation token expiration period to 1 hour
TokensValidation::setConfirmationTokenExpirationDelay(60 * 60); // seconds
//these lines should be called after preparation.
...
//verify the data entered by the user.
...
$invitation = TokensValidation::checkInvitationToken(
token: $_GET['token'],
whatFor: "administration",
thenAccept: true
);
if (!$invitation->isValidationSucceed()) {
die("INVITATION_INVALID");
}
...
//insert the data entered by the user.
//performe some actions
echo "invitation accepted";
...