From 2de4d22498a86dfadd1b473e9b043a7b39559540 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Sat, 7 Jan 2023 11:14:13 +0530 Subject: [PATCH] Improve AuthDataValidator API (#6) * Update prettier config to use editorconfig * Update package.json * Update AuthDataValidator to allow setting bot token later * Update docs --- .prettierrc | 2 - README.md | 4 +- package.json | 2 +- packages/react/README.md | 61 ++++++++++--------- packages/react/docs/README.md | 4 +- .../docs/interfaces/CreateScriptOptions.md | 18 +++--- .../react/docs/interfaces/LoginButtonProps.md | 18 +++--- .../react/docs/interfaces/TelegramAuthData.md | 14 ++--- packages/server/README.md | 17 +++--- packages/server/docs/README.md | 10 +-- .../server/docs/classes/AuthDataValidator.md | 16 ++--- .../interfaces/AuthDataValidatorOptions.md | 28 +++++---- .../docs/interfaces/TelegramUserData.md | 16 ++--- packages/server/src/AuthDataValidator.ts | 14 ++--- 14 files changed, 115 insertions(+), 109 deletions(-) diff --git a/.prettierrc b/.prettierrc index 70c49c5..2e6c597 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,11 +1,9 @@ { "arrowParens": "always", "bracketSpacing": true, - "endOfLine": "lf", "printWidth": 120, "semi": true, "singleQuote": true, - "tabWidth": 4, "trailingComma": "es5", "useTabs": true } diff --git a/README.md b/README.md index bbf22e3..5e61bd3 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,5 @@ This is a mono-repo for the `@telgram-auth/*` packages. It contains the following packages: -- [`@telgram-auth/react`](./packages/react) - React component for the [Telegram Login Widget](https://core.telegram.org/widgets/login). -- [`@telgram-auth/server`](./packages/server) - TS/JS class to validate the data received from Telegram Login Widget. +- [`@telgram-auth/react`](./packages/react) - React component for the [Telegram Login Widget](https://core.telegram.org/widgets/login). +- [`@telgram-auth/server`](./packages/server) - TS/JS class to validate the data received from Telegram Login Widget. diff --git a/package.json b/package.json index 530e730..6230d3f 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,10 @@ "scripts": { "build": "turbo run build", "build:p": "turbo run build --filter=./packages/*", + "buuild:docs": "turbo run build:docs", "kick-off": "pnpm clean && pnpm build:p", "clean": "turbo run clean", "deploy": "turbo run deploy", - "docs": "turbo run build:docs", "dev": "turbo run dev --parallel", "lint": "turbo run lint", "typecheck": "turbo run typecheck", diff --git a/packages/react/README.md b/packages/react/README.md index a8c25a9..4e57abc 100644 --- a/packages/react/README.md +++ b/packages/react/README.md @@ -4,7 +4,7 @@ ## Documentation -- [Reference](./docs/README.md) +- [Reference](./docs/README.md) ## Install @@ -25,49 +25,52 @@ pnpm add @telgram-auth/react If you specify `authCallbackUrl`, the user will be redirected to the specified URL after the authentication process is completed. You must validate the data sent to the URL as query params before authorizing the user. + ```tsx title=src/App.tsx import { LoginButton } from '@telgram-auth/react'; function App() { - return ( -
- -
- ); + return ( +
+ +
+ ); } ``` ### with `onAuthCallback` + ```jsx title=src/App.jsx import { LoginButton } from '@telgram-auth/react'; function App() { - return ( -
- { - console.log(data); - // call your backend here to validate the data and sign in the user - }} - /> -
- ); + return ( +
+ { + console.log(data); + // call your backend here to validate the data and sign in the user + }} + /> +
+ ); } ``` ## Validation -You can use `@telegram-auth/server` server-side to validate the data. +You can use [`@telegram-auth/server`](../server) server-side to validate the data. + ```ts title=validate.ts import { urlStrToAuthDataMap, AuthDataValidator } from '@telgram-auth/server'; @@ -76,12 +79,12 @@ const validator = new AuthDataValidator({ botToken: process.env.BOT_TOKEN }); const data = urlStrToAuthDataMap(request.url); try { - const user = await validator.validate(data); + const user = await validator.validate(data); - // The data is now valid and you can sign in the user. + // The data is now valid and you can sign in the user. - console.log(user); + console.log(user); } catch (error) { - console.error(error); + console.error(error); } ``` diff --git a/packages/react/docs/README.md b/packages/react/docs/README.md index 6ce62a7..a1f7652 100644 --- a/packages/react/docs/README.md +++ b/packages/react/docs/README.md @@ -41,7 +41,7 @@ A React component that renders the Telegram login button. #### Defined in -[LoginButton.tsx:23](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/LoginButton.tsx#L23) +[LoginButton.tsx:23](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/LoginButton.tsx#L23) ___ @@ -69,4 +69,4 @@ A script element #### Defined in -[createScript.ts:11](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/createScript.ts#L11) +[createScript.ts:11](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/createScript.ts#L11) diff --git a/packages/react/docs/interfaces/CreateScriptOptions.md b/packages/react/docs/interfaces/CreateScriptOptions.md index d9e8f93..e03beeb 100644 --- a/packages/react/docs/interfaces/CreateScriptOptions.md +++ b/packages/react/docs/interfaces/CreateScriptOptions.md @@ -38,7 +38,7 @@ The username of the bot that will be used to authenticate the user. #### Defined in -[types.ts:25](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L25) +[types.ts:25](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L25) ___ @@ -54,7 +54,7 @@ The URL where the auth data from Telegram will be sent. #### Defined in -[types.ts:20](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L20) +[types.ts:20](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L20) ___ @@ -74,7 +74,7 @@ The size of the button. #### Defined in -[types.ts:32](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L32) +[types.ts:32](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L32) ___ @@ -90,7 +90,7 @@ The radius of the button corners. #### Defined in -[types.ts:37](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L37) +[types.ts:37](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L37) ___ @@ -110,7 +110,7 @@ The language of the button. #### Defined in -[types.ts:44](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L44) +[types.ts:44](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L44) ___ @@ -140,7 +140,7 @@ The callback function that will be called when the user is authenticated. #### Defined in -[types.ts:49](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L49) +[types.ts:49](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L49) ___ @@ -160,7 +160,7 @@ The access level that the bot will request. #### Defined in -[types.ts:56](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L56) +[types.ts:56](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L56) ___ @@ -180,7 +180,7 @@ true #### Defined in -[types.ts:63](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L63) +[types.ts:63](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L63) ___ @@ -196,4 +196,4 @@ The version of the Telegram widget to deal with browser caching. #### Defined in -[types.ts:68](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L68) +[types.ts:68](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L68) diff --git a/packages/react/docs/interfaces/LoginButtonProps.md b/packages/react/docs/interfaces/LoginButtonProps.md index 456a3c2..c47cf56 100644 --- a/packages/react/docs/interfaces/LoginButtonProps.md +++ b/packages/react/docs/interfaces/LoginButtonProps.md @@ -32,7 +32,7 @@ The username of the bot that will be used to authenticate the user. #### Defined in -[types.ts:25](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L25) +[types.ts:25](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L25) ___ @@ -44,7 +44,7 @@ The URL where the auth data from Telegram will be sent. #### Defined in -[types.ts:20](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L20) +[types.ts:20](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L20) ___ @@ -60,7 +60,7 @@ The size of the button. #### Defined in -[types.ts:32](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L32) +[types.ts:32](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L32) ___ @@ -72,7 +72,7 @@ The radius of the button corners. #### Defined in -[types.ts:37](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L37) +[types.ts:37](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L37) ___ @@ -88,7 +88,7 @@ The language of the button. #### Defined in -[types.ts:44](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L44) +[types.ts:44](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L44) ___ @@ -114,7 +114,7 @@ The callback function that will be called when the user is authenticated. #### Defined in -[types.ts:49](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L49) +[types.ts:49](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L49) ___ @@ -130,7 +130,7 @@ The access level that the bot will request. #### Defined in -[types.ts:56](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L56) +[types.ts:56](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L56) ___ @@ -146,7 +146,7 @@ true #### Defined in -[types.ts:63](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L63) +[types.ts:63](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L63) ___ @@ -158,4 +158,4 @@ The version of the Telegram widget to deal with browser caching. #### Defined in -[types.ts:68](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L68) +[types.ts:68](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L68) diff --git a/packages/react/docs/interfaces/TelegramAuthData.md b/packages/react/docs/interfaces/TelegramAuthData.md index 64d6bfa..a7f9c20 100644 --- a/packages/react/docs/interfaces/TelegramAuthData.md +++ b/packages/react/docs/interfaces/TelegramAuthData.md @@ -28,7 +28,7 @@ https://core.telegram.org/widgets/login#receiving-authorization-data #### Defined in -[types.ts:7](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L7) +[types.ts:7](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L7) ___ @@ -38,7 +38,7 @@ ___ #### Defined in -[types.ts:8](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L8) +[types.ts:8](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L8) ___ @@ -48,7 +48,7 @@ ___ #### Defined in -[types.ts:9](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L9) +[types.ts:9](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L9) ___ @@ -58,7 +58,7 @@ ___ #### Defined in -[types.ts:10](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L10) +[types.ts:10](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L10) ___ @@ -68,7 +68,7 @@ ___ #### Defined in -[types.ts:11](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L11) +[types.ts:11](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L11) ___ @@ -78,7 +78,7 @@ ___ #### Defined in -[types.ts:12](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L12) +[types.ts:12](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L12) ___ @@ -88,4 +88,4 @@ ___ #### Defined in -[types.ts:13](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/react/src/types.ts#L13) +[types.ts:13](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/react/src/types.ts#L13) diff --git a/packages/server/README.md b/packages/server/README.md index 0fbaed1..f5612d9 100644 --- a/packages/server/README.md +++ b/packages/server/README.md @@ -1,11 +1,13 @@ # @telgram-auth/server -`@telgram-auth/server` exports a TS/JS class ([`AuthDataValidator`](./docs/classes/AuthDataValidator.md)) to validate the data received from [Telegram Login Widget](https://core.telegram.org/widgets/login#checking-authorization). +_Zero dependency package to validate the data received from Telegram Login Widget or Web App, compatible with Node, serverless edge networks and web workers._ + +`@telgram-auth/server` exports a TS/JS class ([`AuthDataValidator`](./docs/classes/AuthDataValidator.md)) to validate the data received from [Telegram Login Widget](https://core.telegram.org/widgets/login#checking-authorization) and [Telegram Web Apps](https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app). It also exports some [utility functions](./docs/README.md#functions) to prepare the data for validation. ## Documentation -- [Reference](./docs/README.md) +- [Reference](./docs/README.md) ## Install @@ -22,6 +24,7 @@ pnpm add @telgram-auth/server ## Usage + ```ts title=validate.ts import { urlStrToAuthDataMap, AuthDataValidator } from '@telgram-auth/server'; @@ -32,13 +35,13 @@ const validator = new AuthDataValidator({ botToken: process.env.BOT_TOKEN }); const data = urlStrToAuthDataMap(request.url); try { - // validate the data - const user = await validator.validate(data); + // 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. + // The data is now valid and you can sign in the user. - console.log(user); + console.log(user); } catch (error) { - console.error(error); + console.error(error); } ``` diff --git a/packages/server/docs/README.md b/packages/server/docs/README.md index d3a4fd8..ffbf58d 100644 --- a/packages/server/docs/README.md +++ b/packages/server/docs/README.md @@ -46,7 +46,7 @@ A Uint8Array of the hexString. #### Defined in -[utils/hexStringToArrayBuffer.ts:7](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/hexStringToArrayBuffer.ts#L7) +[utils/hexStringToArrayBuffer.ts:7](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/hexStringToArrayBuffer.ts#L7) ___ @@ -71,7 +71,7 @@ A new Map object with the entries from the searchParams object. #### Defined in -[utils/searchParamsToAuthDataMap.ts:10](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/searchParamsToAuthDataMap.ts#L10) +[utils/searchParamsToAuthDataMap.ts:10](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/searchParamsToAuthDataMap.ts#L10) ___ @@ -95,7 +95,7 @@ A new Map object with the entries of the object passed in. #### Defined in -[utils/objectToAuthDataMap.ts:9](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/objectToAuthDataMap.ts#L9) +[utils/objectToAuthDataMap.ts:9](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/objectToAuthDataMap.ts#L9) ___ @@ -120,7 +120,7 @@ A Map object with the key/value pairs from the URL's query string. #### Defined in -[utils/urlStrToAuthDataMap.ts:8](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/urlStrToAuthDataMap.ts#L8) +[utils/urlStrToAuthDataMap.ts:8](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/urlStrToAuthDataMap.ts#L8) ## Type Aliases @@ -132,4 +132,4 @@ Shape of the data to be passed AuthDataValidator.validate(). #### Defined in -[utils/types.ts:4](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/types.ts#L4) +[utils/types.ts:4](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/types.ts#L4) diff --git a/packages/server/docs/classes/AuthDataValidator.md b/packages/server/docs/classes/AuthDataValidator.md index 9e16e85..9a5b7df 100644 --- a/packages/server/docs/classes/AuthDataValidator.md +++ b/packages/server/docs/classes/AuthDataValidator.md @@ -27,7 +27,7 @@ https://core.telegram.org/widgets/login#checking-authorization ### constructor -• **new AuthDataValidator**(`«destructured»`) +• **new AuthDataValidator**(`«destructured»?`) #### Parameters @@ -37,7 +37,7 @@ https://core.telegram.org/widgets/login#checking-authorization #### Defined in -[AuthDataValidator.ts:53](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L53) +[AuthDataValidator.ts:55](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L55) ## Methods @@ -59,7 +59,7 @@ Sets the bot token to be used for validating the data #### Defined in -[AuthDataValidator.ts:94](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L94) +[AuthDataValidator.ts:96](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L96) ___ @@ -81,7 +81,7 @@ Sets the crypto to be used for validating the data #### Defined in -[AuthDataValidator.ts:105](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L105) +[AuthDataValidator.ts:107](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L107) ___ @@ -103,7 +103,7 @@ Sets the encoder to be used for encoding the data #### Defined in -[AuthDataValidator.ts:116](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L116) +[AuthDataValidator.ts:118](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L118) ___ @@ -126,7 +126,7 @@ which is the number of seconds after which the data is considered invalid. #### Defined in -[AuthDataValidator.ts:129](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L129) +[AuthDataValidator.ts:131](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L131) ___ @@ -148,7 +148,7 @@ This function sets the throwIfEmptyData property of the class. #### Defined in -[AuthDataValidator.ts:140](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L140) +[AuthDataValidator.ts:142](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L142) ___ @@ -182,4 +182,4 @@ The validated data. #### Defined in -[AuthDataValidator.ts:154](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L154) +[AuthDataValidator.ts:156](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L156) diff --git a/packages/server/docs/interfaces/AuthDataValidatorOptions.md b/packages/server/docs/interfaces/AuthDataValidatorOptions.md index ff1cde2..a99700f 100644 --- a/packages/server/docs/interfaces/AuthDataValidatorOptions.md +++ b/packages/server/docs/interfaces/AuthDataValidatorOptions.md @@ -16,19 +16,21 @@ ### botToken -• **botToken**: `string` +• `Optional` **botToken**: `string` -The bot token to be used for validating the data +The bot token to be used for validating the data. + +If you don't pass this here, you'll need to set it later using `setBotToken()`. #### Defined in -[AuthDataValidator.ts:8](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L8) +[AuthDataValidator.ts:10](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L10) -___ +--- ### subtleCrypto -• `Optional` **subtleCrypto**: [`SubtleCrypto`]( https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto ) +• `Optional` **subtleCrypto**: [`SubtleCrypto`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto) The crypto object to be used for validating the data @@ -38,21 +40,21 @@ https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto #### Defined in -[AuthDataValidator.ts:15](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L15) +[AuthDataValidator.ts:17](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L17) -___ +--- ### encoder -• `Optional` **encoder**: [`TextEncoder`]( https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder ) +• `Optional` **encoder**: [`TextEncoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) The encoder to be used for encoding the data #### Defined in -[AuthDataValidator.ts:20](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L20) +[AuthDataValidator.ts:22](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L22) -___ +--- ### inValidateDataAfter @@ -66,9 +68,9 @@ The time in seconds after which the data should be considered invalid #### Defined in -[AuthDataValidator.ts:27](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L27) +[AuthDataValidator.ts:29](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L29) -___ +--- ### throwIfEmptyData @@ -82,4 +84,4 @@ true #### Defined in -[AuthDataValidator.ts:34](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/AuthDataValidator.ts#L34) +[AuthDataValidator.ts:36](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/AuthDataValidator.ts#L36) diff --git a/packages/server/docs/interfaces/TelegramUserData.md b/packages/server/docs/interfaces/TelegramUserData.md index 22db951..4ce64cc 100644 --- a/packages/server/docs/interfaces/TelegramUserData.md +++ b/packages/server/docs/interfaces/TelegramUserData.md @@ -29,7 +29,7 @@ https://core.telegram.org/widgets/login#receiving-authorization-data #### Defined in -[utils/types.ts:12](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/types.ts#L12) +[utils/types.ts:12](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/types.ts#L12) ___ @@ -39,7 +39,7 @@ ___ #### Defined in -[utils/types.ts:13](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/types.ts#L13) +[utils/types.ts:13](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/types.ts#L13) ___ @@ -49,7 +49,7 @@ ___ #### Defined in -[utils/types.ts:14](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/types.ts#L14) +[utils/types.ts:14](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/types.ts#L14) ___ @@ -59,7 +59,7 @@ ___ #### Defined in -[utils/types.ts:15](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/types.ts#L15) +[utils/types.ts:15](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/types.ts#L15) ___ @@ -69,7 +69,7 @@ ___ #### Defined in -[utils/types.ts:16](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/types.ts#L16) +[utils/types.ts:16](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/types.ts#L16) ___ @@ -79,7 +79,7 @@ ___ #### Defined in -[utils/types.ts:18](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/types.ts#L18) +[utils/types.ts:18](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/types.ts#L18) ___ @@ -89,7 +89,7 @@ ___ #### Defined in -[utils/types.ts:19](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/types.ts#L19) +[utils/types.ts:19](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/types.ts#L19) ___ @@ -99,4 +99,4 @@ ___ #### Defined in -[utils/types.ts:20](https://github.com/manzoorwanijk/telegram-auth/blob/bb906d2/packages/server/src/utils/types.ts#L20) +[utils/types.ts:20](https://github.com/manzoorwanijk/telegram-auth/blob/7c2bc06/packages/server/src/utils/types.ts#L20) diff --git a/packages/server/src/AuthDataValidator.ts b/packages/server/src/AuthDataValidator.ts index 45be940..f2f3096 100644 --- a/packages/server/src/AuthDataValidator.ts +++ b/packages/server/src/AuthDataValidator.ts @@ -3,9 +3,11 @@ import type { AuthDataMap, TelegramUserData } from './utils'; export interface AuthDataValidatorOptions { /** - * The bot token to be used for validating the data + * The bot token to be used for validating the data. + * + * If you don't pass this here, you'll need to set it later using `setBotToken()`. */ - botToken: string; + botToken?: string; /** * The crypto object to be used for validating the data @@ -51,12 +53,12 @@ export class AuthDataValidator { protected throwIfEmptyData!: boolean; constructor({ - botToken, + botToken = '', subtleCrypto, encoder, inValidateDataAfter = 86400, throwIfEmptyData = true, - }: AuthDataValidatorOptions) { + }: AuthDataValidatorOptions = {}) { this.setBotToken(botToken); this.setCrypto(this.assertValidCrypto(subtleCrypto)); this.setEncoder(encoder || new TextEncoder()); @@ -193,9 +195,7 @@ export class AuthDataValidator { let data: T; if (isWebAppData) { - const user = authData.get('user') || '{}'; - - data = authData.get('user') ? JSON.parse(user.toString()) : {}; + data = authData.has('user') ? JSON.parse(authData.get('user')?.toString() || '{}') : {}; } else { data = Object.fromEntries(authData.entries()) as T; }