Skip to content

Configuration ​

Environment ​

Secrets and per-environment values come from runtime config, so one build works everywhere:

VariableNeeded for
NUXT_APP_SECRETSealing sessions in production (at least 32 characters).
NUXT_PUBLIC_TELEGRAM_AUTH_CLIENT_IDTelegram Login. Your bot's numeric id.
NUXT_TELEGRAM_AUTH_CLIENT_SECRETThe redirect login only.
NUXT_TELEGRAM_AUTH_BOT_TOKENThe old login widget only.
NUXT_TELEGRAM_AUTH_SESSION_PASSWORDOptional. Seals sessions instead of a secret derived from NUXT_APP_SECRET.
NUXT_TELEGRAM_AUTH_REDIRECT_URLOptional. The full callback URL, when the one built from the request is wrong.

In development the module warns about missing or mismatched values, such as a client ID that belongs to a different bot than the bot token.

Module options ​

Set these under telegramAuth in nuxt.config.ts:

ts
export default defineNuxtConfig({
  telegramAuth: {
    session: { maxAge: 60 * 60 * 24 * 7 },
    scope: ["phone"],
    loginPath: "/login",
  },
});
OptionDefaultDescription
session.name'nuxt-telegram-auth'Cookie name.
session.maxAge86400Session length in seconds, counted from the login.
session.cookie{}Changes to the cookie's HttpOnly, Secure, SameSite=Lax defaults, e.g. { secure: false } for testing over plain HTTP on your network.
scope[]Adds 'phone' or 'write'. Profile data is always requested, because the user id comes with it.
loginPath'/login'See Page protection.
redirectAfterLogin'/'See Page protection.
protectAllfalseSee Page protection.
compat.useUserSessiontrueRegisters the deprecated 1.x composable.
compat.migrateLegacyCookietrueConverts a valid 1.x tg_user cookie into a session.
sessionMaxAgeDeprecated name of session.maxAge.

Routes ​

RouteDescription
GET /api/telegram/sessionThe current session, or { loggedIn: false }.
DELETE /api/telegram/sessionLogs out.
POST /api/telegram/nonceStarts a popup login.
POST /api/telegram/sdkFinishes a popup login with the id_token from Telegram.
GET /auth/telegramStarts the redirect login.
GET /auth/telegram/callbackFinishes the redirect login.
POST /api/telegram/sessionLogs in with the old widget.
GET /api/telegram/callbackFinishes an old widget login in redirect mode.

Every answer carries Cache-Control: private, no-store, so a cache never hands one visitor's session to another. The login routes accept only JSON from your own site.