Skip to content

Latest commit

 

History

History
48 lines (33 loc) · 1.5 KB

README.md

File metadata and controls

48 lines (33 loc) · 1.5 KB

@telegram-auth/server

Zero dependency package to validate the data received from Telegram Login Widget or Web App, compatible with Node, serverless edge networks and web workers.

@telegram-auth/server exports a TS/JS class (AuthDataValidator) to validate the data received from Telegram Login Widget and Telegram Web Apps. It also exports some utility functions to prepare the data for validation.

Documentation

Install

# npm
npm install @telegram-auth/server

# yarn
yarn add @telegram-auth/server

# with pnpm
pnpm add @telegram-auth/server

Usage

import { AuthDataValidator } from '@telegram-auth/server';
import { urlStrToAuthDataMap } from '@telegram-auth/server/utils';

// initialize the validator with your bot token
const validator = new AuthDataValidator({ botToken: process.env.BOT_TOKEN });

// convert the data from the URL to a map
const data = urlStrToAuthDataMap(request.url);

try {
    // validate the data by passing the map to the validator
    const user = await validator.validate(data);

    // The data is now valid and you can sign in the user.

    console.log(user);
} catch (error) {
    console.error(error);
}