Skip to content

Commit

Permalink
Improve AuthDataValidator API (#6)
Browse files Browse the repository at this point in the history
* Update prettier config to use editorconfig

* Update package.json

* Update AuthDataValidator to allow setting bot token later

* Update docs
  • Loading branch information
manzoorwanijk authored Jan 7, 2023
1 parent 7c2bc06 commit 2de4d22
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 109 deletions.
2 changes: 0 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"arrowParens": "always",
"bracketSpacing": true,
"endOfLine": "lf",
"printWidth": 120,
"semi": true,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "es5",
"useTabs": true
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 32 additions & 29 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Documentation

- [Reference](./docs/README.md)
- [Reference](./docs/README.md)

## Install

Expand All @@ -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.

<!-- prettier-ignore -->
```tsx title=src/App.tsx
import { LoginButton } from '@telgram-auth/react';

function App() {
return (
<div className="App">
<LoginButton
botUsername={process.env.BOT_USERNAME}
authCallbackUrl="/path/to/callback/url"
buttonSize="large" // "large" | "medium" | "small"
cornerRadius={5} // 0 - 20
showAvatar={true} // true | false
lang="en"
/>
</div>
);
return (
<div className="App">
<LoginButton
botUsername={process.env.BOT_USERNAME}
authCallbackUrl="/path/to/callback/url"
buttonSize="large" // "large" | "medium" | "small"
cornerRadius={5} // 0 - 20
showAvatar={true} // true | false
lang="en"
/>
</div>
);
}
```

### with `onAuthCallback`

<!-- prettier-ignore -->
```jsx title=src/App.jsx
import { LoginButton } from '@telgram-auth/react';

function App() {
return (
<div className="App">
<LoginButton
botUsername={process.env.BOT_USERNAME}
onAuthCallback={(data) => {
console.log(data);
// call your backend here to validate the data and sign in the user
}}
/>
</div>
);
return (
<div className="App">
<LoginButton
botUsername={process.env.BOT_USERNAME}
onAuthCallback={(data) => {
console.log(data);
// call your backend here to validate the data and sign in the user
}}
/>
</div>
);
}
```

## 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.

<!-- prettier-ignore -->
```ts title=validate.ts
import { urlStrToAuthDataMap, AuthDataValidator } from '@telgram-auth/server';

Expand All @@ -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);
}
```
4 changes: 2 additions & 2 deletions packages/react/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

___

Expand Down Expand Up @@ -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)
18 changes: 9 additions & 9 deletions packages/react/docs/interfaces/CreateScriptOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand Down Expand Up @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)
18 changes: 9 additions & 9 deletions packages/react/docs/interfaces/LoginButtonProps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)
14 changes: 7 additions & 7 deletions packages/react/docs/interfaces/TelegramAuthData.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)
Loading

0 comments on commit 2de4d22

Please sign in to comment.