1. Go to this page and download the library: Download kilogram/auth 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/ */
kilogram / auth example snippets
use Kilogram\Auth\Validator;
$data = $_GET;
$validator = new Validator($_ENV['TELEGRAM_BOT_TOKEN']);
if ($validator->isValidLoginWidget($data)) {
echo "Authenticated. User ID: " . $data['id'];
} else {
echo "Authentication failed";
}
use Kilogram\Auth\Validator;
use Kilogram\Auth\Exceptions\InvalidDataException;
use Kilogram\Auth\Exceptions\ValidationException;
$data = $_GET;
$validator = new Validator($_ENV['TELEGRAM_BOT_TOKEN']);
try {
$validator->validateLoginWidget($data);
echo "Authenticated. Hello " . ($data['first_name'] ?? 'user');
} catch (InvalidDataException $e) {
// Developer error: invalid input format (e.g. missing "hash")
echo "Bad request: " . $e->getMessage();
} catch (ValidationException $e) {
// Invalid signature: possible tampering
echo "Authentication failed";
}
use Kilogram\Auth\Validator;
$initData = $_POST['initData'];
$validator = new Validator($_ENV['TELEGRAM_BOT_TOKEN']);
if ($validator->isValidWebApp($initData)) {
echo "Web App authenticated";
} else {
echo "Invalid initData";
}
use Kilogram\Auth\Validator;
use Kilogram\Auth\Exceptions\InvalidDataException;
use Kilogram\Auth\Exceptions\ValidationException;
$initData = $_POST['initData'];
$validator = new Validator($_ENV['TELEGRAM_BOT_TOKEN']);
try {
$validator->validateWebApp($initData);
echo "Web App authenticated";
} catch (InvalidDataException $e) {
// Developer error: initData format is broken / empty
echo "Bad request: " . $e->getMessage();
} catch (ValidationException $e) {
// Invalid signature
echo "Authentication failed";
}