@telegram-login-ultimate/react 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,32 +14,6 @@ yarn add @telegram-login-ultimate/react
14
14
  pnpm add @telegram-login-ultimate/react
15
15
  ```
16
16
 
17
- ## Features
17
+ ## Documentation
18
18
 
19
- - TypeScript support out of the box - full typed package
20
- - Simple usage - import hook and use
21
-
22
- ## Usage
23
-
24
- ```jsx
25
- import React from 'react';
26
- import ReactDOM from 'react-dom';
27
- import { useTelegramLogin } from '@telegram-login-ultimate/react';
28
-
29
- const App = () => {
30
- const [openPopup, { isPending }] = useTelegramLogin({
31
- botId: <your_bot_id>,
32
- onSuccess: (user) => handleSuccess(user),
33
- onFail: () => handleFail(),
34
- })
35
-
36
- return (
37
- <button disabled={isPending} onClick={openPopup}>
38
- {isPending ? 'Popup opened' : 'Open popup'}
39
- </button>
40
- )
41
- }
42
-
43
- const root = ReactDOM.createRoot(document.getElementById('root'));
44
- root.render(<App/>);
45
- ```
19
+ You can find documentation [here](https://telegram-login-ultimate.vercel.app/packages/react.html)
@@ -0,0 +1,36 @@
1
+ import { LoginOptions, TelegramLoginClient, TelegramLoginClient as TelegramLoginClient$1, TelegramLoginClientOptions, TelegramUserData, isAuthenticationError } from "@telegram-login-ultimate/core";
2
+
3
+ //#region src/hook.d.ts
4
+ type UseTelegramLoginOptions = LoginOptions;
5
+ interface UseTelegramLoginReturn {
6
+ /**
7
+ * Flag indicating whether the login process is pending.
8
+ */
9
+ isPending: boolean;
10
+ /**
11
+ * Function to start the login process.
12
+ */
13
+ start: () => void;
14
+ }
15
+ declare const useTelegramLogin: ({
16
+ botId,
17
+ onError,
18
+ onSuccess,
19
+ onStart
20
+ }: UseTelegramLoginOptions) => UseTelegramLoginReturn;
21
+ //#endregion
22
+ //#region src/provider.d.ts
23
+ interface TelegramLoginProviderProps {
24
+ children: React.ReactNode;
25
+ /**
26
+ * Telegram login client instance.
27
+ */
28
+ client: TelegramLoginClient$1;
29
+ }
30
+ declare const TelegramLoginProvider: ({
31
+ client,
32
+ children
33
+ }: TelegramLoginProviderProps) => React.ReactElement;
34
+ //#endregion
35
+ export { TelegramLoginClient, type TelegramLoginClientOptions, TelegramLoginProvider, type TelegramLoginProviderProps, type TelegramUserData, type UseTelegramLoginOptions, type UseTelegramLoginReturn, type isAuthenticationError, useTelegramLogin };
36
+ //# sourceMappingURL=index.d.mts.map
package/dist/index.d.ts CHANGED
@@ -1,19 +1,36 @@
1
- interface TelegramLoginData {
2
- id: number;
3
- first_name: string;
4
- last_name?: string;
5
- username?: string;
6
- photo_url?: string;
7
- auth_date: number;
8
- hash: string;
1
+ import { LoginOptions, TelegramLoginClient, TelegramLoginClient as TelegramLoginClient$1, TelegramLoginClientOptions, TelegramUserData, isAuthenticationError } from "@telegram-login-ultimate/core";
2
+
3
+ //#region src/hook.d.ts
4
+ type UseTelegramLoginOptions = LoginOptions;
5
+ interface UseTelegramLoginReturn {
6
+ /**
7
+ * Flag indicating whether the login process is pending.
8
+ */
9
+ isPending: boolean;
10
+ /**
11
+ * Function to start the login process.
12
+ */
13
+ start: () => void;
9
14
  }
10
- interface UseTelegramLoginOptions {
11
- botId: number;
12
- onSuccess?: (data: TelegramLoginData) => unknown;
13
- onFail?: () => unknown;
15
+ declare const useTelegramLogin: ({
16
+ botId,
17
+ onError,
18
+ onSuccess,
19
+ onStart
20
+ }: UseTelegramLoginOptions) => UseTelegramLoginReturn;
21
+ //#endregion
22
+ //#region src/provider.d.ts
23
+ interface TelegramLoginProviderProps {
24
+ children: React.ReactNode;
25
+ /**
26
+ * Telegram login client instance.
27
+ */
28
+ client: TelegramLoginClient$1;
14
29
  }
15
- declare const useTelegramLogin: ({ botId, onFail, onSuccess, }: UseTelegramLoginOptions) => readonly [() => void, {
16
- readonly isPending: boolean;
17
- }];
18
-
19
- export { type TelegramLoginData, type UseTelegramLoginOptions, useTelegramLogin };
30
+ declare const TelegramLoginProvider: ({
31
+ client,
32
+ children
33
+ }: TelegramLoginProviderProps) => React.ReactElement;
34
+ //#endregion
35
+ export { TelegramLoginClient, type TelegramLoginClientOptions, TelegramLoginProvider, type TelegramLoginProviderProps, type TelegramUserData, type UseTelegramLoginOptions, type UseTelegramLoginReturn, type isAuthenticationError, useTelegramLogin };
36
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`react`);c=s(c);let l=require(`react/jsx-runtime`);l=s(l);let u=require(`@telegram-login-ultimate/core`);u=s(u);const d=(0,c.createContext)(void 0),f=({botId:e,onError:t,onSuccess:n,onStart:r})=>{let[i,a]=(0,c.useState)(!1),o=(0,c.useContext)(d);if(!o)throw Error(`useTelegramLogin must be used within a TelegramLoginProvider`);let s=()=>{a(!0),r?.()},l=e=>{a(!1),t?.(e)},u=e=>{a(!1),n?.(e)};return{start:()=>{o.login({botId:e,onError:l,onSuccess:u,onStart:s})},isPending:i}},p=({client:e,children:t})=>(0,l.jsx)(d.Provider,{value:e,children:t});Object.defineProperty(exports,`TelegramLoginClient`,{enumerable:!0,get:function(){return u.TelegramLoginClient}}),exports.TelegramLoginProvider=p,exports.useTelegramLogin=f;
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/context.ts","../src/hook.ts","../src/provider.tsx"],"sourcesContent":["import type { TelegramLoginClient } from '@telegram-login-ultimate/core'\n\nimport { createContext } from 'react'\n\nexport const TelegramLoginContext = createContext<\n TelegramLoginClient | undefined\n>(undefined)\n","import type {\n LoginOptions,\n TelegramUserData,\n} from '@telegram-login-ultimate/core'\n\nimport { useContext, useState } from 'react'\n\nimport { TelegramLoginContext } from './context'\n\nexport type UseTelegramLoginOptions = LoginOptions\n\nexport interface UseTelegramLoginReturn {\n /**\n * Flag indicating whether the login process is pending.\n */\n isPending: boolean\n\n /**\n * Function to start the login process.\n */\n start: () => void\n}\n\nexport const useTelegramLogin = ({\n botId,\n onError,\n onSuccess,\n onStart,\n}: UseTelegramLoginOptions): UseTelegramLoginReturn => {\n const [isPending, setIsPending] = useState(false)\n const client = useContext(TelegramLoginContext)\n\n if (!client) {\n throw new Error(\n 'useTelegramLogin must be used within a TelegramLoginProvider',\n )\n }\n\n const handleStart = (): void => {\n setIsPending(true)\n onStart?.()\n }\n\n const handleError = (error: unknown): void => {\n setIsPending(false)\n onError?.(error)\n }\n\n const handleSuccess = (data: TelegramUserData): void => {\n setIsPending(false)\n onSuccess?.(data)\n }\n\n const start = (): void => {\n client.login({\n botId,\n onError: handleError,\n onSuccess: handleSuccess,\n onStart: handleStart,\n })\n }\n\n return { start, isPending } as const\n}\n","import type { TelegramLoginClient } from '@telegram-login-ultimate/core'\n\nimport { TelegramLoginContext } from './context'\n\nexport interface TelegramLoginProviderProps {\n children: React.ReactNode\n\n /**\n * Telegram login client instance.\n */\n client: TelegramLoginClient\n}\n\nexport const TelegramLoginProvider = ({\n client,\n children,\n}: TelegramLoginProviderProps): React.ReactElement => {\n return (\n <TelegramLoginContext.Provider value={client}>\n {children}\n </TelegramLoginContext.Provider>\n )\n}\n"],"mappings":"4lBAIA,MAAa,GAAA,EAAA,EAAA,eAEX,IAAA,GAAU,CCiBC,GAAoB,CAC/B,QACA,UACA,YACA,aACqD,CACrD,GAAM,CAAC,EAAW,IAAA,EAAA,EAAA,UAAyB,GAAM,CAC3C,GAAA,EAAA,EAAA,YAAoB,EAAqB,CAE/C,GAAI,CAAC,EACH,MAAU,MACR,+DACD,CAGH,IAAM,MAA0B,CAC9B,EAAa,GAAK,CAClB,KAAW,EAGP,EAAe,GAAyB,CAC5C,EAAa,GAAM,CACnB,IAAU,EAAM,EAGZ,EAAiB,GAAiC,CACtD,EAAa,GAAM,CACnB,IAAY,EAAK,EAYnB,MAAO,CAAE,UATiB,CACxB,EAAO,MAAM,CACX,QACA,QAAS,EACT,UAAW,EACX,QAAS,EACV,CAAC,EAGY,YAAW,ECjDhB,GAAyB,CACpC,SACA,eAGE,EAAA,EAAA,KAAC,EAAqB,SAAA,CAAS,MAAO,EACnC,YAC6B"}
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ import{createContext as e,useContext as t,useState as n}from"react";import{jsx as r}from"react/jsx-runtime";import{TelegramLoginClient as i}from"@telegram-login-ultimate/core";const a=e(void 0),o=({botId:e,onError:r,onSuccess:i,onStart:o})=>{let[s,c]=n(!1),l=t(a);if(!l)throw Error(`useTelegramLogin must be used within a TelegramLoginProvider`);let u=()=>{c(!0),o?.()},d=e=>{c(!1),r?.(e)},f=e=>{c(!1),i?.(e)};return{start:()=>{l.login({botId:e,onError:d,onSuccess:f,onStart:u})},isPending:s}},s=({client:e,children:t})=>r(a.Provider,{value:e,children:t});export{i as TelegramLoginClient,s as TelegramLoginProvider,o as useTelegramLogin};
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/context.ts","../src/hook.ts","../src/provider.tsx"],"sourcesContent":["import type { TelegramLoginClient } from '@telegram-login-ultimate/core'\n\nimport { createContext } from 'react'\n\nexport const TelegramLoginContext = createContext<\n TelegramLoginClient | undefined\n>(undefined)\n","import type {\n LoginOptions,\n TelegramUserData,\n} from '@telegram-login-ultimate/core'\n\nimport { useContext, useState } from 'react'\n\nimport { TelegramLoginContext } from './context'\n\nexport type UseTelegramLoginOptions = LoginOptions\n\nexport interface UseTelegramLoginReturn {\n /**\n * Flag indicating whether the login process is pending.\n */\n isPending: boolean\n\n /**\n * Function to start the login process.\n */\n start: () => void\n}\n\nexport const useTelegramLogin = ({\n botId,\n onError,\n onSuccess,\n onStart,\n}: UseTelegramLoginOptions): UseTelegramLoginReturn => {\n const [isPending, setIsPending] = useState(false)\n const client = useContext(TelegramLoginContext)\n\n if (!client) {\n throw new Error(\n 'useTelegramLogin must be used within a TelegramLoginProvider',\n )\n }\n\n const handleStart = (): void => {\n setIsPending(true)\n onStart?.()\n }\n\n const handleError = (error: unknown): void => {\n setIsPending(false)\n onError?.(error)\n }\n\n const handleSuccess = (data: TelegramUserData): void => {\n setIsPending(false)\n onSuccess?.(data)\n }\n\n const start = (): void => {\n client.login({\n botId,\n onError: handleError,\n onSuccess: handleSuccess,\n onStart: handleStart,\n })\n }\n\n return { start, isPending } as const\n}\n","import type { TelegramLoginClient } from '@telegram-login-ultimate/core'\n\nimport { TelegramLoginContext } from './context'\n\nexport interface TelegramLoginProviderProps {\n children: React.ReactNode\n\n /**\n * Telegram login client instance.\n */\n client: TelegramLoginClient\n}\n\nexport const TelegramLoginProvider = ({\n client,\n children,\n}: TelegramLoginProviderProps): React.ReactElement => {\n return (\n <TelegramLoginContext.Provider value={client}>\n {children}\n </TelegramLoginContext.Provider>\n )\n}\n"],"mappings":"gLAIA,MAAa,EAAuB,EAElC,IAAA,GAAU,CCiBC,GAAoB,CAC/B,QACA,UACA,YACA,aACqD,CACrD,GAAM,CAAC,EAAW,GAAgB,EAAS,GAAM,CAC3C,EAAS,EAAW,EAAqB,CAE/C,GAAI,CAAC,EACH,MAAU,MACR,+DACD,CAGH,IAAM,MAA0B,CAC9B,EAAa,GAAK,CAClB,KAAW,EAGP,EAAe,GAAyB,CAC5C,EAAa,GAAM,CACnB,IAAU,EAAM,EAGZ,EAAiB,GAAiC,CACtD,EAAa,GAAM,CACnB,IAAY,EAAK,EAYnB,MAAO,CAAE,UATiB,CACxB,EAAO,MAAM,CACX,QACA,QAAS,EACT,UAAW,EACX,QAAS,EACV,CAAC,EAGY,YAAW,ECjDhB,GAAyB,CACpC,SACA,cAGE,EAAC,EAAqB,SAAA,CAAS,MAAO,EACnC,YAC6B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@telegram-login-ultimate/react",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "Ultimate tool for working with Telegram login API with TypeScript support in your React application",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -19,8 +19,8 @@
19
19
  ],
20
20
  "homepage": "https://github.com/notcodev/telegram-login-ultimate",
21
21
  "bugs": "https://github.com/notcodev/telegram-login-ultimate/issues",
22
- "main": "dist/cjs/index.js",
23
- "module": "dist/esm/index.js",
22
+ "main": "dist/index.js",
23
+ "module": "dist/index.mjs",
24
24
  "types": "dist/index.d.ts",
25
25
  "keywords": [
26
26
  "telegram",
@@ -38,17 +38,18 @@
38
38
  }
39
39
  },
40
40
  "devDependencies": {
41
- "@siberiacancode/builder": "^1.3.11",
42
41
  "@types/react": "^19.1.8",
43
42
  "@types/react-dom": "^19.1.6",
44
- "@vitejs/plugin-react": "^4.3.0",
43
+ "@vitejs/plugin-react": "^5.0.2",
45
44
  "react": "^19.1.0",
46
45
  "react-dom": "^19.1.0",
47
- "shx": "^0.3.4",
48
- "vite": "^5.2.13"
46
+ "vite": "^7.1.4"
47
+ },
48
+ "dependencies": {
49
+ "@telegram-login-ultimate/core": "2.0.0"
49
50
  },
50
51
  "scripts": {
51
- "build": "shx rm -rf dist && rollup -c --bundleConfigAsCjs",
52
+ "build": "tsdown",
52
53
  "lint": "eslint . --ext ts --ext tsx --no-error-on-unmatched-pattern --fix",
53
54
  "dev": "cd ./playground && vite"
54
55
  }
@@ -1,3 +0,0 @@
1
- /* @license @telegram-login-ultimate/react v1.0.1 */
2
- "use strict";var e=require("./use-telegram-login.js");require("react"),exports.useTelegramLogin=e.useTelegramLogin;
3
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -1,3 +0,0 @@
1
- /* @license @telegram-login-ultimate/react v1.0.1 */
2
- "use strict";var t=require("react");function r(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}function e(t,r,e,n,o,i,a){try{var u=t[i](a),c=u.value}catch(t){return void e(t)}u.done?r(c):Promise.resolve(c).then(n,o)}function n(){n=function(){return r};var t,r={},e=Object.prototype,o=e.hasOwnProperty,i=Object.defineProperty||function(t,r,e){t[r]=e.value},a="function"==typeof Symbol?Symbol:{},u=a.iterator||"@@iterator",c=a.asyncIterator||"@@asyncIterator",l=a.toStringTag||"@@toStringTag";function h(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{h({},"")}catch(t){h=function(t,r,e){return t[r]=e}}function s(t,r,e,n){var o=r&&r.prototype instanceof m?r:m,a=Object.create(o.prototype),u=new P(n||[]);return i(a,"_invoke",{value:S(t,e,u)}),a}function f(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}r.wrap=s;var p="suspendedStart",d="suspendedYield",y="executing",v="completed",g={};function m(){}function w(){}function b(){}var L={};h(L,u,(function(){return this}));var x=Object.getPrototypeOf,E=x&&x(x(k([])));E&&E!==e&&o.call(E,u)&&(L=E);var _=b.prototype=m.prototype=Object.create(L);function O(t){["next","throw","return"].forEach((function(r){h(t,r,(function(t){return this._invoke(r,t)}))}))}function j(t,r){function e(n,i,a,u){var c=f(t[n],t,i);if("throw"!==c.type){var l=c.arg,h=l.value;return h&&"object"==typeof h&&o.call(h,"__await")?r.resolve(h.__await).then((function(t){e("next",t,a,u)}),(function(t){e("throw",t,a,u)})):r.resolve(h).then((function(t){l.value=t,a(l)}),(function(t){return e("throw",t,a,u)}))}u(c.arg)}var n;i(this,"_invoke",{value:function(t,o){function i(){return new r((function(r,n){e(t,o,r,n)}))}return n=n?n.then(i,i):i()}})}function S(r,e,n){var o=p;return function(i,a){if(o===y)throw Error("Generator is already running");if(o===v){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var u=n.delegate;if(u){var c=I(u,n);if(c){if(c===g)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===p)throw o=v,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=y;var l=f(r,e,n);if("normal"===l.type){if(o=n.done?v:d,l.arg===g)continue;return{value:l.arg,done:n.done}}"throw"===l.type&&(o=v,n.method="throw",n.arg=l.arg)}}}function I(r,e){var n=e.method,o=r.iterator[n];if(o===t)return e.delegate=null,"throw"===n&&r.iterator.return&&(e.method="return",e.arg=t,I(r,e),"throw"===e.method)||"return"!==n&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+n+"' method")),g;var i=f(o,r.iterator,e.arg);if("throw"===i.type)return e.method="throw",e.arg=i.arg,e.delegate=null,g;var a=i.arg;return a?a.done?(e[r.resultName]=a.value,e.next=r.nextLoc,"return"!==e.method&&(e.method="next",e.arg=t),e.delegate=null,g):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,g)}function T(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function F(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function P(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(T,this),this.reset(!0)}function k(r){if(r||""===r){var e=r[u];if(e)return e.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var n=-1,i=function e(){for(;++n<r.length;)if(o.call(r,n))return e.value=r[n],e.done=!1,e;return e.value=t,e.done=!0,e};return i.next=i}}throw new TypeError(typeof r+" is not iterable")}return w.prototype=b,i(_,"constructor",{value:b,configurable:!0}),i(b,"constructor",{value:w,configurable:!0}),w.displayName=h(b,l,"GeneratorFunction"),r.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===w||"GeneratorFunction"===(r.displayName||r.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,b):(t.__proto__=b,h(t,l,"GeneratorFunction")),t.prototype=Object.create(_),t},r.awrap=function(t){return{__await:t}},O(j.prototype),h(j.prototype,c,(function(){return this})),r.AsyncIterator=j,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new j(s(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},O(_),h(_,l,"Generator"),h(_,u,(function(){return this})),h(_,"toString",(function(){return"[object Generator]"})),r.keys=function(t){var r=Object(t),e=[];for(var n in r)e.push(n);return e.reverse(),function t(){for(;e.length;){var n=e.pop();if(n in r)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=k,P.prototype={constructor:P,reset:function(r){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(F),!r)for(var e in this)"t"===e.charAt(0)&&o.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(r){if(this.done)throw r;var e=this;function n(n,o){return u.type="throw",u.arg=r,e.next=n,o&&(e.method="next",e.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],u=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var c=o.call(a,"catchLoc"),l=o.call(a,"finallyLoc");if(c&&l){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!l)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc<=this.prev&&o.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,g):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),g},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),F(e),g}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;F(e)}return o}}throw Error("illegal catch attempt")},delegateYield:function(r,e,n){return this.delegate={iterator:k(r),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=t),g}},r}function o(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,r){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var n,o,i,a,u=[],c=!0,l=!1;try{if(i=(e=e.call(t)).next,0===r);else for(;!(c=(n=i.call(e)).done)&&(u.push(n.value),u.length!==r);c=!0);}catch(t){l=!0,o=t}finally{try{if(!c&&null!=e.return&&(a=e.return(),Object(a)!==a))return}finally{if(l)throw o}}return u}}(t,e)||function(t,e){if(t){if("string"==typeof t)return r(t,e);var n={}.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(t,e):void 0}}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var i="https://oauth.telegram.org";exports.useTelegramLogin=function(r){var a=r.botId,u=r.onFail,c=r.onSuccess,l=o(t.useState(!1),2),h=l[0],s=l[1],f=t.useRef({});return[function(){var t=Math.max(0,(screen.width-550)/2)+screen.availWidth,r=Math.max(0,(screen.height-470)/2)+screen.availHeight,o=i+"/auth?bot_id="+encodeURIComponent(a)+"&origin="+encodeURIComponent(location.origin||location.protocol+"//"+location.hostname)+"&return_to="+encodeURIComponent(location.href),l=window.open(o,"telegram_oauth_bot"+a,"width=550,height=470,left="+t+",top="+r+",status=0,location=0,menubar=0,toolbar=0");function h(t){var r;try{r=JSON.parse(t.data)}catch(t){return}a in f.current&&t.source===f.current[a].window&&"auth_result"===r.event&&(s(!1),p(r.result))}function p(t){a in f.current&&(f.current[a].authFinished||(null==c||c(t),f.current[a].authFinished=!0,window.removeEventListener("message",h)))}function d(){var t;return t=n().mark((function t(r){return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",fetch(i+"/auth/get?bot_id="+encodeURIComponent(r.botId),{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8","X-Requested-With":"XMLHttpRequest"},credentials:"include"}).then((function(t){return t.json()})));case 1:case"end":return t.stop()}}),t)})),d=function(){var r=this,n=arguments;return new Promise((function(o,i){var a=t.apply(r,n);function u(t){e(a,o,i,u,c,"next",t)}function c(t){e(a,o,i,u,c,"throw",t)}u(void 0)}))},d.apply(this,arguments)}f.current[a]={window:l,authFinished:!1},l&&(s(!0),window.addEventListener("message",h),l.focus(),function t(r){if(!(r in f.current))return;var e=f.current[r];if(!e.window||e.window.closed)return function(t){return d.apply(this,arguments)}({botId:r}).then((function(t){"user"in t?p(t.user):r in f.current&&!f.current[r].authFinished&&(null==u||u())})).catch(console.error).finally((function(){return s(!1)}));setTimeout((function(){return t(r)}),100)}(a))},{isPending:h}]};
3
- //# sourceMappingURL=use-telegram-login.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-telegram-login.js","sources":["../../../src/hooks/use-telegram-login.ts"],"sourcesContent":["import { useRef, useState } from 'react'\n\nconst POPUP_WIDTH = 550\nconst POPUP_HEIGHT = 470\nconst POPUP_ORIGIN = 'https://oauth.telegram.org'\n\nexport interface TelegramLoginData {\n id: number\n first_name: string\n last_name?: string\n username?: string\n photo_url?: string\n auth_date: number\n hash: string\n}\n\nexport interface UseTelegramLoginOptions {\n botId: number\n onSuccess?: (data: TelegramLoginData) => unknown\n onFail?: () => unknown\n}\n\nexport const useTelegramLogin = ({\n botId,\n onFail,\n onSuccess,\n}: UseTelegramLoginOptions) => {\n const [isPending, setIsPending] = useState(false)\n const popups = useRef<\n Record<number, { window: Window | null; authFinished: boolean }>\n >({})\n\n return [\n () => {\n const width = POPUP_WIDTH\n const height = POPUP_HEIGHT\n const left = Math.max(0, (screen.width - width) / 2) + screen.availWidth\n const top = Math.max(0, (screen.height - height) / 2) + screen.availHeight\n\n const popupUrl =\n POPUP_ORIGIN +\n '/auth?bot_id=' +\n encodeURIComponent(botId) +\n '&origin=' +\n encodeURIComponent(\n location.origin || location.protocol + '//' + location.hostname,\n ) +\n '&return_to=' +\n encodeURIComponent(location.href)\n\n const popup = window.open(\n popupUrl,\n 'telegram_oauth_bot' + botId,\n 'width=' +\n width +\n ',height=' +\n height +\n ',left=' +\n left +\n ',top=' +\n top +\n ',status=0,location=0,menubar=0,toolbar=0',\n )\n\n popups.current[botId] = {\n window: popup,\n authFinished: false,\n }\n\n if (popup) {\n setIsPending(true)\n window.addEventListener('message', onMessage)\n popup.focus()\n checkClose(botId)\n }\n\n function onMessage(event: MessageEvent<string>) {\n let data: { event: string; result: TelegramLoginData }\n\n try {\n data = JSON.parse(event.data)\n } catch (error) {\n return\n }\n\n if (!(botId in popups.current)) return\n\n if (event.source !== popups.current[botId].window) return\n if (data.event === 'auth_result') {\n setIsPending(false)\n onAuthDone(data.result)\n }\n }\n\n function onAuthDone(authData: TelegramLoginData) {\n if (!(botId in popups.current)) return\n\n if (popups.current[botId].authFinished) return\n\n onSuccess?.(authData)\n\n popups.current[botId].authFinished = true\n window.removeEventListener('message', onMessage)\n }\n\n function checkClose(botId: number) {\n if (!(botId in popups.current)) return\n\n const currentPopup = popups.current[botId]\n\n if (!currentPopup.window || currentPopup.window.closed) {\n return getAuthData({ botId })\n .then((res) => {\n if ('user' in res) onAuthDone(res.user)\n else if (\n botId in popups.current &&\n !popups.current[botId].authFinished\n )\n onFail?.()\n })\n .catch(console.error)\n .finally(() => setIsPending(false))\n }\n\n setTimeout(() => checkClose(botId), 100)\n }\n\n async function getAuthData(options: { botId: number }): Promise<\n ({ user: TelegramLoginData } | { error: string }) & {\n html: string\n origin: string\n }\n > {\n return fetch(\n POPUP_ORIGIN +\n '/auth/get' +\n '?bot_id=' +\n encodeURIComponent(options.botId),\n {\n method: 'POST',\n headers: {\n 'Content-Type':\n 'application/x-www-form-urlencoded; charset=UTF-8',\n 'X-Requested-With': 'XMLHttpRequest',\n },\n credentials: 'include',\n },\n ).then((res) => res.json())\n }\n },\n { isPending },\n ] as const\n}\n"],"names":["POPUP_ORIGIN","_ref","botId","onFail","onSuccess","_useState2","_slicedToArray","useState","isPending","setIsPending","popups","useRef","left","Math","max","screen","width","availWidth","top","height","availHeight","popupUrl","encodeURIComponent","location","origin","protocol","hostname","href","popup","window","open","onMessage","event","data","JSON","parse","error","current","source","onAuthDone","result","authData","authFinished","removeEventListener","_getAuthData","_regeneratorRuntime","mark","_callee","options","wrap","_context","prev","next","abrupt","fetch","method","headers","credentials","then","res","json","stop","apply","this","arguments","addEventListener","focus","checkClose","currentPopup","closed","_x","getAuthData","user","console","setTimeout"],"mappings":";s+OAEA,IAEMA,EAAe,sDAkBW,SAAHC,GAIE,IAH7BC,EAAKD,EAALC,MACAC,EAAMF,EAANE,OACAC,EAASH,EAATG,UAEiDC,EAAAC,EAAfC,EAAQA,UAAC,GAAM,GAA1CC,EAASH,EAAA,GAAEI,EAAYJ,EAAA,GACxBK,EAASC,SAEb,CAAA,GAEF,MAAO,CACL,WACE,IAEMC,EAAOC,KAAKC,IAAI,GAAIC,OAAOC,MAlCnB,KAkCoC,GAAKD,OAAOE,WACxDC,EAAML,KAAKC,IAAI,GAAIC,OAAOI,OAlCjB,KAkCoC,GAAKJ,OAAOK,YAEzDC,EACJrB,EACA,gBACAsB,mBAAmBpB,GACnB,WACAoB,mBACEC,SAASC,QAAUD,SAASE,SAAW,KAAOF,SAASG,UAEzD,cACAJ,mBAAmBC,SAASI,MAExBC,EAAQC,OAAOC,KACnBT,EACA,qBAAuBnB,EACvB,6BAKEU,EACA,QACAM,EACA,4CAeJ,SAASa,EAAUC,GACjB,IAAIC,EAEJ,IACEA,EAAOC,KAAKC,MAAMH,EAAMC,KACzB,CAAC,MAAOG,GACP,MACF,CAEMlC,KAASQ,EAAO2B,SAElBL,EAAMM,SAAW5B,EAAO2B,QAAQnC,GAAO2B,QACxB,gBAAfI,EAAKD,QACPvB,GAAa,GACb8B,EAAWN,EAAKO,QAEpB,CAEA,SAASD,EAAWE,GACZvC,KAASQ,EAAO2B,UAElB3B,EAAO2B,QAAQnC,GAAOwC,eAE1BtC,SAAAA,EAAYqC,GAEZ/B,EAAO2B,QAAQnC,GAAOwC,cAAe,EACrCb,OAAOc,oBAAoB,UAAWZ,IACxC,CAwB0B,SAAAa,UAqBzB,SArByBC,IAAAC,MAA1B,SAAAC,EAA2BC,GAA0B,OAAAH,IAAAI,MAAA,SAAAC,GAAA,cAAAA,EAAAC,KAAAD,EAAAE,MAAA,KAAA,EAAA,OAAAF,EAAAG,gBAM5CC,MACLtD,sBAGEsB,mBAAmB0B,EAAQ9C,OAC7B,CACEqD,OAAQ,OACRC,QAAS,CACP,eACE,mDACF,mBAAoB,kBAEtBC,YAAa,YAEfC,MAAK,SAACC,GAAG,OAAKA,EAAIC,MAAO,KAAA,KAAA,EAAA,IAAA,MAAA,OAAAV,EAAAW,OAAA,GAAAd,EAC5B,IArByBH,gLAqBzBA,EAAAkB,MAAAC,KAAAC,UAAA,CApFDtD,EAAO2B,QAAQnC,GAAS,CACtB2B,OAAQD,EACRc,cAAc,GAGZd,IACFnB,GAAa,GACboB,OAAOoC,iBAAiB,UAAWlC,GACnCH,EAAMsC,QAiCR,SAASC,EAAWjE,GAClB,KAAMA,KAASQ,EAAO2B,SAAU,OAEhC,IAAM+B,EAAe1D,EAAO2B,QAAQnC,GAEpC,IAAKkE,EAAavC,QAAUuC,EAAavC,OAAOwC,OAC9C,OAcH,SAEyBC,GAAA,OAAA1B,EAAAkB,MAAAC,KAAAC,UAAA,CAhBfO,CAAY,CAAErE,MAAAA,IAClBwD,MAAK,SAACC,GACD,SAAUA,EAAKpB,EAAWoB,EAAIa,MAEhCtE,KAASQ,EAAO2B,UACf3B,EAAO2B,QAAQnC,GAAOwC,eAEvBvC,SAAAA,cAEGsE,QAAQrC,gBACN,WAAA,OAAM3B,GAAa,MAGhCiE,YAAW,WAAA,OAAMP,EAAWjE,EAAM,GAAE,IACtC,CApDEiE,CAAWjE,GA4Ef,EACA,CAAEM,UAAAA,GAEN"}
package/dist/cjs/index.js DELETED
@@ -1,3 +0,0 @@
1
- /* @license @telegram-login-ultimate/react v1.0.1 */
2
- "use strict";var e=require("./hooks/use-telegram-login.js");require("react"),exports.useTelegramLogin=e.useTelegramLogin;
3
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -1,3 +0,0 @@
1
- /* @license @telegram-login-ultimate/react v1.0.1 */
2
- export{useTelegramLogin}from"./use-telegram-login.js";import"react";
3
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -1,3 +0,0 @@
1
- /* @license @telegram-login-ultimate/react v1.0.1 */
2
- import{useState as t,useRef as r}from"react";function e(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=Array(r);e<r;e++)n[e]=t[e];return n}function n(t,r,e,n,o,i,a){try{var u=t[i](a),c=u.value}catch(t){return void e(t)}u.done?r(c):Promise.resolve(c).then(n,o)}function o(){o=function(){return r};var t,r={},e=Object.prototype,n=e.hasOwnProperty,i=Object.defineProperty||function(t,r,e){t[r]=e.value},a="function"==typeof Symbol?Symbol:{},u=a.iterator||"@@iterator",c=a.asyncIterator||"@@asyncIterator",l=a.toStringTag||"@@toStringTag";function h(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{h({},"")}catch(t){h=function(t,r,e){return t[r]=e}}function f(t,r,e,n){var o=r&&r.prototype instanceof m?r:m,a=Object.create(o.prototype),u=new T(n||[]);return i(a,"_invoke",{value:S(t,e,u)}),a}function s(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}r.wrap=f;var p="suspendedStart",d="suspendedYield",y="executing",v="completed",g={};function m(){}function w(){}function b(){}var x={};h(x,u,(function(){return this}));var L=Object.getPrototypeOf,E=L&&L(L(k([])));E&&E!==e&&n.call(E,u)&&(x=E);var _=b.prototype=m.prototype=Object.create(x);function O(t){["next","throw","return"].forEach((function(r){h(t,r,(function(t){return this._invoke(r,t)}))}))}function j(t,r){function e(o,i,a,u){var c=s(t[o],t,i);if("throw"!==c.type){var l=c.arg,h=l.value;return h&&"object"==typeof h&&n.call(h,"__await")?r.resolve(h.__await).then((function(t){e("next",t,a,u)}),(function(t){e("throw",t,a,u)})):r.resolve(h).then((function(t){l.value=t,a(l)}),(function(t){return e("throw",t,a,u)}))}u(c.arg)}var o;i(this,"_invoke",{value:function(t,n){function i(){return new r((function(r,o){e(t,n,r,o)}))}return o=o?o.then(i,i):i()}})}function S(r,e,n){var o=p;return function(i,a){if(o===y)throw Error("Generator is already running");if(o===v){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var u=n.delegate;if(u){var c=I(u,n);if(c){if(c===g)continue;return c}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===p)throw o=v,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=y;var l=s(r,e,n);if("normal"===l.type){if(o=n.done?v:d,l.arg===g)continue;return{value:l.arg,done:n.done}}"throw"===l.type&&(o=v,n.method="throw",n.arg=l.arg)}}}function I(r,e){var n=e.method,o=r.iterator[n];if(o===t)return e.delegate=null,"throw"===n&&r.iterator.return&&(e.method="return",e.arg=t,I(r,e),"throw"===e.method)||"return"!==n&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+n+"' method")),g;var i=s(o,r.iterator,e.arg);if("throw"===i.type)return e.method="throw",e.arg=i.arg,e.delegate=null,g;var a=i.arg;return a?a.done?(e[r.resultName]=a.value,e.next=r.nextLoc,"return"!==e.method&&(e.method="next",e.arg=t),e.delegate=null,g):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,g)}function F(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function P(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function T(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(F,this),this.reset(!0)}function k(r){if(r||""===r){var e=r[u];if(e)return e.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var o=-1,i=function e(){for(;++o<r.length;)if(n.call(r,o))return e.value=r[o],e.done=!1,e;return e.value=t,e.done=!0,e};return i.next=i}}throw new TypeError(typeof r+" is not iterable")}return w.prototype=b,i(_,"constructor",{value:b,configurable:!0}),i(b,"constructor",{value:w,configurable:!0}),w.displayName=h(b,l,"GeneratorFunction"),r.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===w||"GeneratorFunction"===(r.displayName||r.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,b):(t.__proto__=b,h(t,l,"GeneratorFunction")),t.prototype=Object.create(_),t},r.awrap=function(t){return{__await:t}},O(j.prototype),h(j.prototype,c,(function(){return this})),r.AsyncIterator=j,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new j(f(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},O(_),h(_,l,"Generator"),h(_,u,(function(){return this})),h(_,"toString",(function(){return"[object Generator]"})),r.keys=function(t){var r=Object(t),e=[];for(var n in r)e.push(n);return e.reverse(),function t(){for(;e.length;){var n=e.pop();if(n in r)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=k,T.prototype={constructor:T,reset:function(r){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(P),!r)for(var e in this)"t"===e.charAt(0)&&n.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(r){if(this.done)throw r;var e=this;function o(n,o){return u.type="throw",u.arg=r,e.next=n,o&&(e.method="next",e.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],u=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var c=n.call(a,"catchLoc"),l=n.call(a,"finallyLoc");if(c&&l){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(c){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!l)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var o=this.tryEntries[e];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,g):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),g},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),P(e),g}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;P(e)}return o}}throw Error("illegal catch attempt")},delegateYield:function(r,e,n){return this.delegate={iterator:k(r),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=t),g}},r}function i(t,r){return function(t){if(Array.isArray(t))return t}(t)||function(t,r){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var n,o,i,a,u=[],c=!0,l=!1;try{if(i=(e=e.call(t)).next,0===r);else for(;!(c=(n=i.call(e)).done)&&(u.push(n.value),u.length!==r);c=!0);}catch(t){l=!0,o=t}finally{try{if(!c&&null!=e.return&&(a=e.return(),Object(a)!==a))return}finally{if(l)throw o}}return u}}(t,r)||function(t,r){if(t){if("string"==typeof t)return e(t,r);var n={}.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(t,r):void 0}}(t,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var a="https://oauth.telegram.org",u=function(e){var u=e.botId,c=e.onFail,l=e.onSuccess,h=i(t(!1),2),f=h[0],s=h[1],p=r({});return[function(){var t=Math.max(0,(screen.width-550)/2)+screen.availWidth,r=Math.max(0,(screen.height-470)/2)+screen.availHeight,e=a+"/auth?bot_id="+encodeURIComponent(u)+"&origin="+encodeURIComponent(location.origin||location.protocol+"//"+location.hostname)+"&return_to="+encodeURIComponent(location.href),i=window.open(e,"telegram_oauth_bot"+u,"width=550,height=470,left="+t+",top="+r+",status=0,location=0,menubar=0,toolbar=0");function h(t){var r;try{r=JSON.parse(t.data)}catch(t){return}u in p.current&&t.source===p.current[u].window&&"auth_result"===r.event&&(s(!1),f(r.result))}function f(t){u in p.current&&(p.current[u].authFinished||(null==l||l(t),p.current[u].authFinished=!0,window.removeEventListener("message",h)))}function d(){var t;return t=o().mark((function t(r){return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",fetch(a+"/auth/get?bot_id="+encodeURIComponent(r.botId),{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8","X-Requested-With":"XMLHttpRequest"},credentials:"include"}).then((function(t){return t.json()})));case 1:case"end":return t.stop()}}),t)})),d=function(){var r=this,e=arguments;return new Promise((function(o,i){var a=t.apply(r,e);function u(t){n(a,o,i,u,c,"next",t)}function c(t){n(a,o,i,u,c,"throw",t)}u(void 0)}))},d.apply(this,arguments)}p.current[u]={window:i,authFinished:!1},i&&(s(!0),window.addEventListener("message",h),i.focus(),function t(r){if(!(r in p.current))return;var e=p.current[r];if(!e.window||e.window.closed)return function(t){return d.apply(this,arguments)}({botId:r}).then((function(t){"user"in t?f(t.user):r in p.current&&!p.current[r].authFinished&&(null==c||c())})).catch(console.error).finally((function(){return s(!1)}));setTimeout((function(){return t(r)}),100)}(u))},{isPending:f}]};export{u as useTelegramLogin};
3
- //# sourceMappingURL=use-telegram-login.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-telegram-login.js","sources":["../../../src/hooks/use-telegram-login.ts"],"sourcesContent":["import { useRef, useState } from 'react'\n\nconst POPUP_WIDTH = 550\nconst POPUP_HEIGHT = 470\nconst POPUP_ORIGIN = 'https://oauth.telegram.org'\n\nexport interface TelegramLoginData {\n id: number\n first_name: string\n last_name?: string\n username?: string\n photo_url?: string\n auth_date: number\n hash: string\n}\n\nexport interface UseTelegramLoginOptions {\n botId: number\n onSuccess?: (data: TelegramLoginData) => unknown\n onFail?: () => unknown\n}\n\nexport const useTelegramLogin = ({\n botId,\n onFail,\n onSuccess,\n}: UseTelegramLoginOptions) => {\n const [isPending, setIsPending] = useState(false)\n const popups = useRef<\n Record<number, { window: Window | null; authFinished: boolean }>\n >({})\n\n return [\n () => {\n const width = POPUP_WIDTH\n const height = POPUP_HEIGHT\n const left = Math.max(0, (screen.width - width) / 2) + screen.availWidth\n const top = Math.max(0, (screen.height - height) / 2) + screen.availHeight\n\n const popupUrl =\n POPUP_ORIGIN +\n '/auth?bot_id=' +\n encodeURIComponent(botId) +\n '&origin=' +\n encodeURIComponent(\n location.origin || location.protocol + '//' + location.hostname,\n ) +\n '&return_to=' +\n encodeURIComponent(location.href)\n\n const popup = window.open(\n popupUrl,\n 'telegram_oauth_bot' + botId,\n 'width=' +\n width +\n ',height=' +\n height +\n ',left=' +\n left +\n ',top=' +\n top +\n ',status=0,location=0,menubar=0,toolbar=0',\n )\n\n popups.current[botId] = {\n window: popup,\n authFinished: false,\n }\n\n if (popup) {\n setIsPending(true)\n window.addEventListener('message', onMessage)\n popup.focus()\n checkClose(botId)\n }\n\n function onMessage(event: MessageEvent<string>) {\n let data: { event: string; result: TelegramLoginData }\n\n try {\n data = JSON.parse(event.data)\n } catch (error) {\n return\n }\n\n if (!(botId in popups.current)) return\n\n if (event.source !== popups.current[botId].window) return\n if (data.event === 'auth_result') {\n setIsPending(false)\n onAuthDone(data.result)\n }\n }\n\n function onAuthDone(authData: TelegramLoginData) {\n if (!(botId in popups.current)) return\n\n if (popups.current[botId].authFinished) return\n\n onSuccess?.(authData)\n\n popups.current[botId].authFinished = true\n window.removeEventListener('message', onMessage)\n }\n\n function checkClose(botId: number) {\n if (!(botId in popups.current)) return\n\n const currentPopup = popups.current[botId]\n\n if (!currentPopup.window || currentPopup.window.closed) {\n return getAuthData({ botId })\n .then((res) => {\n if ('user' in res) onAuthDone(res.user)\n else if (\n botId in popups.current &&\n !popups.current[botId].authFinished\n )\n onFail?.()\n })\n .catch(console.error)\n .finally(() => setIsPending(false))\n }\n\n setTimeout(() => checkClose(botId), 100)\n }\n\n async function getAuthData(options: { botId: number }): Promise<\n ({ user: TelegramLoginData } | { error: string }) & {\n html: string\n origin: string\n }\n > {\n return fetch(\n POPUP_ORIGIN +\n '/auth/get' +\n '?bot_id=' +\n encodeURIComponent(options.botId),\n {\n method: 'POST',\n headers: {\n 'Content-Type':\n 'application/x-www-form-urlencoded; charset=UTF-8',\n 'X-Requested-With': 'XMLHttpRequest',\n },\n credentials: 'include',\n },\n ).then((res) => res.json())\n }\n },\n { isPending },\n ] as const\n}\n"],"names":["POPUP_ORIGIN","useTelegramLogin","_ref","botId","onFail","onSuccess","_useState2","_slicedToArray","useState","isPending","setIsPending","popups","useRef","left","Math","max","screen","width","availWidth","top","height","availHeight","popupUrl","encodeURIComponent","location","origin","protocol","hostname","href","popup","window","open","onMessage","event","data","JSON","parse","error","current","source","onAuthDone","result","authData","authFinished","removeEventListener","_getAuthData","_regeneratorRuntime","mark","_callee","options","wrap","_context","prev","next","abrupt","fetch","method","headers","credentials","then","res","json","stop","apply","this","arguments","addEventListener","focus","checkClose","currentPopup","closed","_x","getAuthData","user","console","setTimeout"],"mappings":";++OAEA,IAEMA,EAAe,6BAkBRC,EAAmB,SAAHC,GAIE,IAH7BC,EAAKD,EAALC,MACAC,EAAMF,EAANE,OACAC,EAASH,EAATG,UAEiDC,EAAAC,EAAfC,GAAS,GAAM,GAA1CC,EAASH,EAAA,GAAEI,EAAYJ,EAAA,GACxBK,EAASC,EAEb,CAAA,GAEF,MAAO,CACL,WACE,IAEMC,EAAOC,KAAKC,IAAI,GAAIC,OAAOC,MAlCnB,KAkCoC,GAAKD,OAAOE,WACxDC,EAAML,KAAKC,IAAI,GAAIC,OAAOI,OAlCjB,KAkCoC,GAAKJ,OAAOK,YAEzDC,EACJtB,EACA,gBACAuB,mBAAmBpB,GACnB,WACAoB,mBACEC,SAASC,QAAUD,SAASE,SAAW,KAAOF,SAASG,UAEzD,cACAJ,mBAAmBC,SAASI,MAExBC,EAAQC,OAAOC,KACnBT,EACA,qBAAuBnB,EACvB,6BAKEU,EACA,QACAM,EACA,4CAeJ,SAASa,EAAUC,GACjB,IAAIC,EAEJ,IACEA,EAAOC,KAAKC,MAAMH,EAAMC,KACzB,CAAC,MAAOG,GACP,MACF,CAEMlC,KAASQ,EAAO2B,SAElBL,EAAMM,SAAW5B,EAAO2B,QAAQnC,GAAO2B,QACxB,gBAAfI,EAAKD,QACPvB,GAAa,GACb8B,EAAWN,EAAKO,QAEpB,CAEA,SAASD,EAAWE,GACZvC,KAASQ,EAAO2B,UAElB3B,EAAO2B,QAAQnC,GAAOwC,eAE1BtC,SAAAA,EAAYqC,GAEZ/B,EAAO2B,QAAQnC,GAAOwC,cAAe,EACrCb,OAAOc,oBAAoB,UAAWZ,IACxC,CAwB0B,SAAAa,UAqBzB,SArByBC,IAAAC,MAA1B,SAAAC,EAA2BC,GAA0B,OAAAH,IAAAI,MAAA,SAAAC,GAAA,cAAAA,EAAAC,KAAAD,EAAAE,MAAA,KAAA,EAAA,OAAAF,EAAAG,gBAM5CC,MACLvD,sBAGEuB,mBAAmB0B,EAAQ9C,OAC7B,CACEqD,OAAQ,OACRC,QAAS,CACP,eACE,mDACF,mBAAoB,kBAEtBC,YAAa,YAEfC,MAAK,SAACC,GAAG,OAAKA,EAAIC,MAAO,KAAA,KAAA,EAAA,IAAA,MAAA,OAAAV,EAAAW,OAAA,GAAAd,EAC5B,IArByBH,gLAqBzBA,EAAAkB,MAAAC,KAAAC,UAAA,CApFDtD,EAAO2B,QAAQnC,GAAS,CACtB2B,OAAQD,EACRc,cAAc,GAGZd,IACFnB,GAAa,GACboB,OAAOoC,iBAAiB,UAAWlC,GACnCH,EAAMsC,QAiCR,SAASC,EAAWjE,GAClB,KAAMA,KAASQ,EAAO2B,SAAU,OAEhC,IAAM+B,EAAe1D,EAAO2B,QAAQnC,GAEpC,IAAKkE,EAAavC,QAAUuC,EAAavC,OAAOwC,OAC9C,OAcH,SAEyBC,GAAA,OAAA1B,EAAAkB,MAAAC,KAAAC,UAAA,CAhBfO,CAAY,CAAErE,MAAAA,IAClBwD,MAAK,SAACC,GACD,SAAUA,EAAKpB,EAAWoB,EAAIa,MAEhCtE,KAASQ,EAAO2B,UACf3B,EAAO2B,QAAQnC,GAAOwC,eAEvBvC,SAAAA,cAEGsE,QAAQrC,gBACN,WAAA,OAAM3B,GAAa,MAGhCiE,YAAW,WAAA,OAAMP,EAAWjE,EAAM,GAAE,IACtC,CApDEiE,CAAWjE,GA4Ef,EACA,CAAEM,UAAAA,GAEN"}
package/dist/esm/index.js DELETED
@@ -1,3 +0,0 @@
1
- /* @license @telegram-login-ultimate/react v1.0.1 */
2
- export{useTelegramLogin}from"./hooks/use-telegram-login.js";import"react";
3
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}