@telegram-login-ultimate/react 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Erik
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # 🚀 Telegram Login React
2
+
3
+ Ultimate tool for working with Telegram login widget with TypeScript support in your React application
4
+
5
+ ## Installation
6
+
7
+ Install with [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/) or [pnpm](https://pnpm.io)
8
+
9
+ ```shell
10
+ npm install @telegram-login-ultimate/react
11
+ # or
12
+ yarn add @telegram-login-ultimate/react
13
+ # or
14
+ pnpm add @telegram-login-ultimate/react
15
+ ```
16
+
17
+ ## Features
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
+ ```
@@ -0,0 +1,3 @@
1
+ /* @license @telegram-login-ultimate/react v0.1.0 */
2
+ "use strict";var e=require("./use-telegram-login.js");require("react"),exports.useTelegramLogin=e.useTelegramLogin;
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -0,0 +1,3 @@
1
+ /* @license @telegram-login-ultimate/react v0.1.0 */
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 c=t[i](a),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).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:{},c=a.iterator||"@@iterator",u=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),c=new k(n||[]);return i(a,"_invoke",{value:S(t,e,c)}),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",y="suspendedYield",d="executing",v="completed",g={};function m(){}function w(){}function b(){}var x={};h(x,c,(function(){return this}));var L=Object.getPrototypeOf,E=L&&L(L(F([])));E&&E!==e&&o.call(E,c)&&(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(n,i,a,c){var u=f(t[n],t,i);if("throw"!==u.type){var l=u.arg,h=l.value;return h&&"object"==typeof h&&o.call(h,"__await")?r.resolve(h.__await).then((function(t){e("next",t,a,c)}),(function(t){e("throw",t,a,c)})):r.resolve(h).then((function(t){l.value=t,a(l)}),(function(t){return e("throw",t,a,c)}))}c(u.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===d)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 c=n.delegate;if(c){var u=I(c,n);if(u){if(u===g)continue;return u}}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=d;var l=f(r,e,n);if("normal"===l.type){if(o=n.done?v:y,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 P(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function k(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(T,this),this.reset(!0)}function F(r){if(r||""===r){var e=r[c];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,u,(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(_,c,(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=F,k.prototype={constructor:k,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)&&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 c.type="throw",c.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],c=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=o.call(a,"catchLoc"),l=o.call(a,"finallyLoc");if(u&&l){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){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),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:F(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,c=[],u=!0,l=!1;try{if(i=(e=e.call(t)).next,0===r);else for(;!(u=(n=i.call(e)).done)&&(c.push(n.value),c.length!==r);u=!0);}catch(t){l=!0,o=t}finally{try{if(!u&&null!=e.return&&(a=e.return(),Object(a)!==a))return}finally{if(l)throw o}}return c}}(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,c=r.onClose,u=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&&p(r.result)}function p(t){a in f.current&&(f.current[a].authFinished||(s(!1),u(t),f.current[a].authFinished=!0,window.removeEventListener("message",h)))}function y(){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"}}).then((function(t){return t.json()})));case 1:case"end":return t.stop()}}),t)})),y=function(){var r=this,n=arguments;return new Promise((function(o,i){var a=t.apply(r,n);function c(t){e(a,o,i,c,u,"next",t)}function u(t){e(a,o,i,c,u,"throw",t)}c(void 0)}))},y.apply(this,arguments)}f.current[a]={window:l,authFinished:!1},l&&(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 y.apply(this,arguments)}({botId:r}).then((function(t){p(t.user)})).catch((function(){s(!1),c()}));setTimeout((function(){return t(r)}),100)}(a))},{isPending:h}]};
3
+ //# sourceMappingURL=use-telegram-login.js.map
@@ -0,0 +1 @@
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 onClose: () => unknown\n}\n\nexport const useTelegramLogin = ({\n botId,\n onClose,\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 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 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 setIsPending(false)\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 onAuthDone(res.user)\n })\n .catch(() => {\n setIsPending(false)\n onClose()\n })\n }\n\n setTimeout(() => checkClose(botId), 100)\n }\n\n async function getAuthData(options: {\n botId: number\n }): Promise<{ user: TelegramLoginData; html: string; origin: string }> {\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 },\n },\n ).then((res) => res.json())\n }\n },\n { isPending },\n ] as const\n}\n"],"names":["POPUP_ORIGIN","_ref","botId","onClose","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","then","res","json","stop","apply","this","arguments","addEventListener","focus","checkClose","currentPopup","closed","_x","getAuthData","user","setTimeout"],"mappings":";s+OAEA,IAEMA,EAAe,sDAkBW,SAAHC,GAIE,IAH7BC,EAAKD,EAALC,MACAC,EAAOF,EAAPE,QACAC,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,4CAcJ,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,OACPO,EAAWN,EAAKO,OAEpB,CAEA,SAASD,EAAWE,GACZvC,KAASQ,EAAO2B,UAElB3B,EAAO2B,QAAQnC,GAAOwC,eAE1BjC,GAAa,GACbL,EAAUqC,GAEV/B,EAAO2B,QAAQnC,GAAOwC,cAAe,EACrCb,OAAOc,oBAAoB,UAAWZ,IACxC,CAqB0B,SAAAa,UAgBzB,SAhByBC,IAAAC,MAA1B,SAAAC,EAA2BC,GAE1B,OAAAH,IAAAI,MAAA,SAAAC,GAAA,cAAAA,EAAAC,KAAAD,EAAAE,MAAA,KAAA,EAAA,OAAAF,EAAAG,gBACQC,MACLtD,sBAGEsB,mBAAmB0B,EAAQ9C,OAC7B,CACEqD,OAAQ,OACRC,QAAS,CACP,eACE,sDAGNC,MAAK,SAACC,GAAG,OAAKA,EAAIC,MAAO,KAAA,KAAA,EAAA,IAAA,MAAA,OAAAT,EAAAU,OAAA,GAAAb,EAC5B,IAhByBH,gLAgBzBA,EAAAiB,MAAAC,KAAAC,UAAA,CA3EDrD,EAAO2B,QAAQnC,GAAS,CACtB2B,OAAQD,EACRc,cAAc,GAGZd,IACFC,OAAOmC,iBAAiB,UAAWjC,GACnCH,EAAMqC,QAiCR,SAASC,EAAWhE,GAClB,KAAMA,KAASQ,EAAO2B,SAAU,OAEhC,IAAM8B,EAAezD,EAAO2B,QAAQnC,GAEpC,IAAKiE,EAAatC,QAAUsC,EAAatC,OAAOuC,OAC9C,OAWH,SAEyBC,GAAA,OAAAzB,EAAAiB,MAAAC,KAAAC,UAAA,CAbfO,CAAY,CAAEpE,MAAAA,IAClBuD,MAAK,SAACC,GACLnB,EAAWmB,EAAIa,SAEX,OAAC,WACL9D,GAAa,GACbN,GACF,IAGJqE,YAAW,WAAA,OAAMN,EAAWhE,EAAM,GAAE,IACtC,CAjDEgE,CAAWhE,GAoEf,EACA,CAAEM,UAAAA,GAEN"}
@@ -0,0 +1,3 @@
1
+ /* @license @telegram-login-ultimate/react v0.1.0 */
2
+ "use strict";var e=require("./hooks/use-telegram-login.js");require("react"),exports.useTelegramLogin=e.useTelegramLogin;
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -0,0 +1,3 @@
1
+ /* @license @telegram-login-ultimate/react v0.1.0 */
2
+ export{useTelegramLogin}from"./use-telegram-login.js";import"react";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -0,0 +1,3 @@
1
+ /* @license @telegram-login-ultimate/react v0.1.0 */
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 c=t[i](a),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).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:{},c=a.iterator||"@@iterator",u=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),c=new k(n||[]);return i(a,"_invoke",{value:S(t,e,c)}),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",y="suspendedYield",d="executing",v="completed",g={};function m(){}function w(){}function b(){}var x={};h(x,c,(function(){return this}));var L=Object.getPrototypeOf,E=L&&L(L(F([])));E&&E!==e&&n.call(E,c)&&(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,c){var u=s(t[o],t,i);if("throw"!==u.type){var l=u.arg,h=l.value;return h&&"object"==typeof h&&n.call(h,"__await")?r.resolve(h.__await).then((function(t){e("next",t,a,c)}),(function(t){e("throw",t,a,c)})):r.resolve(h).then((function(t){l.value=t,a(l)}),(function(t){return e("throw",t,a,c)}))}c(u.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===d)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 c=n.delegate;if(c){var u=I(c,n);if(u){if(u===g)continue;return u}}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=d;var l=s(r,e,n);if("normal"===l.type){if(o=n.done?v:y,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 P(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 T(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function k(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(P,this),this.reset(!0)}function F(r){if(r||""===r){var e=r[c];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,u,(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(_,c,(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=F,k.prototype={constructor:k,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(T),!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 c.type="throw",c.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],c=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),l=n.call(a,"finallyLoc");if(u&&l){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(u){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),T(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;T(e)}return o}}throw Error("illegal catch attempt")},delegateYield:function(r,e,n){return this.delegate={iterator:F(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,c=[],u=!0,l=!1;try{if(i=(e=e.call(t)).next,0===r);else for(;!(u=(n=i.call(e)).done)&&(c.push(n.value),c.length!==r);u=!0);}catch(t){l=!0,o=t}finally{try{if(!u&&null!=e.return&&(a=e.return(),Object(a)!==a))return}finally{if(l)throw o}}return c}}(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",c=function(e){var c=e.botId,u=e.onClose,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(c)+"&origin="+encodeURIComponent(location.origin||location.protocol+"//"+location.hostname)+"&return_to="+encodeURIComponent(location.href),i=window.open(e,"telegram_oauth_bot"+c,"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}c in p.current&&t.source===p.current[c].window&&"auth_result"===r.event&&f(r.result)}function f(t){c in p.current&&(p.current[c].authFinished||(s(!1),l(t),p.current[c].authFinished=!0,window.removeEventListener("message",h)))}function y(){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"}}).then((function(t){return t.json()})));case 1:case"end":return t.stop()}}),t)})),y=function(){var r=this,e=arguments;return new Promise((function(o,i){var a=t.apply(r,e);function c(t){n(a,o,i,c,u,"next",t)}function u(t){n(a,o,i,c,u,"throw",t)}c(void 0)}))},y.apply(this,arguments)}p.current[c]={window:i,authFinished:!1},i&&(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 y.apply(this,arguments)}({botId:r}).then((function(t){f(t.user)})).catch((function(){s(!1),u()}));setTimeout((function(){return t(r)}),100)}(c))},{isPending:f}]};export{c as useTelegramLogin};
3
+ //# sourceMappingURL=use-telegram-login.js.map
@@ -0,0 +1 @@
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 onClose: () => unknown\n}\n\nexport const useTelegramLogin = ({\n botId,\n onClose,\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 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 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 setIsPending(false)\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 onAuthDone(res.user)\n })\n .catch(() => {\n setIsPending(false)\n onClose()\n })\n }\n\n setTimeout(() => checkClose(botId), 100)\n }\n\n async function getAuthData(options: {\n botId: number\n }): Promise<{ user: TelegramLoginData; html: string; origin: string }> {\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 },\n },\n ).then((res) => res.json())\n }\n },\n { isPending },\n ] as const\n}\n"],"names":["POPUP_ORIGIN","useTelegramLogin","_ref","botId","onClose","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","then","res","json","stop","apply","this","arguments","addEventListener","focus","checkClose","currentPopup","closed","_x","getAuthData","user","setTimeout"],"mappings":";++OAEA,IAEMA,EAAe,6BAkBRC,EAAmB,SAAHC,GAIE,IAH7BC,EAAKD,EAALC,MACAC,EAAOF,EAAPE,QACAC,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,4CAcJ,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,OACPO,EAAWN,EAAKO,OAEpB,CAEA,SAASD,EAAWE,GACZvC,KAASQ,EAAO2B,UAElB3B,EAAO2B,QAAQnC,GAAOwC,eAE1BjC,GAAa,GACbL,EAAUqC,GAEV/B,EAAO2B,QAAQnC,GAAOwC,cAAe,EACrCb,OAAOc,oBAAoB,UAAWZ,IACxC,CAqB0B,SAAAa,UAgBzB,SAhByBC,IAAAC,MAA1B,SAAAC,EAA2BC,GAE1B,OAAAH,IAAAI,MAAA,SAAAC,GAAA,cAAAA,EAAAC,KAAAD,EAAAE,MAAA,KAAA,EAAA,OAAAF,EAAAG,gBACQC,MACLvD,sBAGEuB,mBAAmB0B,EAAQ9C,OAC7B,CACEqD,OAAQ,OACRC,QAAS,CACP,eACE,sDAGNC,MAAK,SAACC,GAAG,OAAKA,EAAIC,MAAO,KAAA,KAAA,EAAA,IAAA,MAAA,OAAAT,EAAAU,OAAA,GAAAb,EAC5B,IAhByBH,gLAgBzBA,EAAAiB,MAAAC,KAAAC,UAAA,CA3EDrD,EAAO2B,QAAQnC,GAAS,CACtB2B,OAAQD,EACRc,cAAc,GAGZd,IACFC,OAAOmC,iBAAiB,UAAWjC,GACnCH,EAAMqC,QAiCR,SAASC,EAAWhE,GAClB,KAAMA,KAASQ,EAAO2B,SAAU,OAEhC,IAAM8B,EAAezD,EAAO2B,QAAQnC,GAEpC,IAAKiE,EAAatC,QAAUsC,EAAatC,OAAOuC,OAC9C,OAWH,SAEyBC,GAAA,OAAAzB,EAAAiB,MAAAC,KAAAC,UAAA,CAbfO,CAAY,CAAEpE,MAAAA,IAClBuD,MAAK,SAACC,GACLnB,EAAWmB,EAAIa,SAEX,OAAC,WACL9D,GAAa,GACbN,GACF,IAGJqE,YAAW,WAAA,OAAMN,EAAWhE,EAAM,GAAE,IACtC,CAjDEgE,CAAWhE,GAoEf,EACA,CAAEM,UAAAA,GAEN"}
@@ -0,0 +1,3 @@
1
+ /* @license @telegram-login-ultimate/react v0.1.0 */
2
+ export{useTelegramLogin}from"./hooks/use-telegram-login.js";import"react";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -0,0 +1,19 @@
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;
9
+ }
10
+ interface UseTelegramLoginOptions {
11
+ botId: number;
12
+ onSuccess: (data: TelegramLoginData) => unknown;
13
+ onClose: () => unknown;
14
+ }
15
+ declare const useTelegramLogin: ({ botId, onClose, onSuccess, }: UseTelegramLoginOptions) => readonly [() => void, {
16
+ readonly isPending: boolean;
17
+ }];
18
+
19
+ export { type TelegramLoginData, type UseTelegramLoginOptions, useTelegramLogin };
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@telegram-login-ultimate/react",
3
+ "version": "1.0.0",
4
+ "description": "Ultimate tool for working with Telegram login API with TypeScript support in your React application",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "nothugofsea",
8
+ "url": "https://github.com/nothugofsea"
9
+ },
10
+ "repository": {
11
+ "url": "https://github.com/nothugofsea/telegram-login-ultimate.git",
12
+ "type": "git"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "files": [
18
+ "dist/**/*"
19
+ ],
20
+ "homepage": "https://github.com/nothugofsea/telegram-login-ultimate",
21
+ "bugs": "https://github.com/nothugofsea/telegram-login-ultimate/issues",
22
+ "main": "dist/cjs/index.js",
23
+ "module": "dist/esm/index.js",
24
+ "types": "dist/index.d.ts",
25
+ "keywords": [
26
+ "telegram",
27
+ "login",
28
+ "widget",
29
+ "react"
30
+ ],
31
+ "peerDependencies": {
32
+ "@types/react": "^18.0.0",
33
+ "react": "^18.0.0"
34
+ },
35
+ "peerDependenciesMeta": {
36
+ "@types/react": {
37
+ "optional": true
38
+ }
39
+ },
40
+ "devDependencies": {
41
+ "@siberiacancode/builder": "^1.3.0",
42
+ "@types/react": "^18.2.56",
43
+ "@types/react-dom": "^18.3.0",
44
+ "@vitejs/plugin-react": "^4.3.0",
45
+ "react": "^18.3.1",
46
+ "react-dom": "^18.3.1",
47
+ "shx": "^0.3.4",
48
+ "vite": "^5.2.13"
49
+ },
50
+ "scripts": {
51
+ "build": "shx rm -rf dist && rollup -c --bundleConfigAsCjs",
52
+ "lint": "eslint . --ext ts --ext tsx --no-error-on-unmatched-pattern --fix",
53
+ "dev": "cd ./playground && vite"
54
+ }
55
+ }