Nipaa's Blog

🔑 Telegram Unified Auth

A minimal PHP solution for authenticating users via Telegram Login Widget and Mini Apps with a single unified flow.

Overview

Telegram Unified Auth is a lightweight PHP implementation that handles Telegram authentication for both the Login Widget and Mini Apps (WebApp). It automatically validates signatures, checks expiration, detects the authentication source, and extracts user data in a unified format — all inside a single lightweight PHP class.

What This Project Offers

File Structure

index.php    # Entry point, shows login UI or user info
webapp.php   # Mini App automatic authentication
auth.php     # TelegramUnifiedAuth class
logout.php   # Session logout

How Authentication Works

1. index.php — Entry Point

2. Telegram Login Widget Flow

  1. User clicks the Telegram Login Widget
  2. Telegram sends user data to the JavaScript callback
  3. AJAX sends the data to webapp.php
  4. TelegramUnifiedAuth validates the signature and expiration
  5. User data is extracted from the validated payload
  6. User info is stored in PHP session
  7. Page reloads with user info displayed

3. Telegram Mini App (WebApp) Flow

Two methods are supported:

Manual (via button)

Automatic (recommended)

Version 1.1.0

Version 1.1.0 changes the internal behavior of the authentication class.

Main improvements:

Old usage:

$is_check = $auth->check($_POST['user']);
$data = $auth->get($_POST['user']);

New usage:

$is_check = $auth->check($_POST['user']);
$data = $auth->get();

Important: call get() only after successful check().

initData (Telegram Mini App)

Mini Apps provide authentication data via:

Telegram.WebApp.initData

Important:

Send it as-is via AJAX:

$.post('/webapp.php', { user: Telegram.WebApp.initData });

Session Data Format

$_SESSION['tg_user'] = [
  'id'         => int,
  'first_name' => string,
  'last_name'  => string,
  'username'   => string,
  'photo_url'  => string
];

Format is identical for both Widget and Mini App.

Security Notes

Requirements

License & Author

MIT License — see LICENSE file

Author: Nipaa
GitHub: https://github.com/Makareene/Telegram-Unified-Auth

This implementation is intentionally minimal and readable — perfect for learning, audits, or integrating into your PHP project with a unified Telegram authentication flow.

Back to Coding