Telegram Better Auth Plugin

0.0.8 · active · verified Wed Apr 22

The `telegram-better-auth` package provides a plugin to seamlessly integrate Telegram login functionality into applications built with the `better-auth` authentication framework. This library, currently at version `0.0.8`, offers a streamlined approach to allowing users to authenticate via their Telegram accounts, managing both the server-side logic for verifying authorization data and the client-side interaction with the Telegram Login Widget. While a formal release cadence is not explicitly stated, the frequent minor version bumps (0.0.x) suggest active development. A key differentiator is its tightly coupled design with `better-auth`, abstracting complex Telegram API interactions, such as the generation of temporary emails based on Telegram IDs. Proper functioning requires a valid Telegram bot token and ensuring the bot is linked to the application's domain via BotFather.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates both server-side and client-side setup for Telegram authentication using `telegram-better-auth` with `better-auth`. It covers plugin configuration and an example of handling authentication data from the Telegram Login Widget.

import { betterAuth } from 'better-auth';
import { createAuthClient } from 'better-auth/client';
import { telegram, telegramClient } from 'telegram-better-auth';

// --- Server-side configuration (auth.ts) ---
export const auth = betterAuth({
  plugins: [
    telegram({
      botToken: process.env.TELEGRAM_BOT_TOKEN ?? '', // Ensure BOT_TOKEN is set in your environment
      getTempEmail: (id) => `${id}@t.me`, // Function to generate a temporary email for Telegram users
    }),
  ],
});

// --- Client-side configuration (auth-client.ts) ---
const authClient = createAuthClient({
  plugins: [telegramClient()],
});

interface TelegramUserAuthData {
  id: number;
  first_name: string;
  last_name?: string;
  username?: string;
  photo_url?: string;
  auth_date: number;
  hash: string;
}

// Example function to handle data from Telegram Login Widget callback
const onTelegramAuth = async (user: TelegramUserAuthData) => {
  try {
    console.log('Attempting Telegram sign-in for user:', user.username || user.id);
    const data = await authClient.signIn.telegram(user);
    console.log('Telegram sign-in successful:', data);
    // Implement session refetch, redirect, etc.
  } catch (error) {
    console.error('Telegram sign-in failed:', error);
    // Handle error (e.g., display message to user)
  }
};

// This function would typically be called from the Telegram Login Widget's callback
// For demonstration, a placeholder:
// For testing, mock a user object (replace with actual data from Telegram):
// const mockTelegramUser: TelegramUserAuthData = {
//   id: 123456789,
//   first_name: 'Test',
//   username: 'testuser',
//   auth_date: Math.floor(Date.now() / 1000),
//   hash: 'mockhash_for_dev_only' // This hash must be generated by Telegram, don't hardcode in production
// };
// onTelegramAuth(mockTelegramUser);

view raw JSON →