UNPKG

dsc-auth

Version:

An Oauth platform built on Qr Codes for DSC

10 lines (9 loc) 2.38 kB
module.exports = { contents: "\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nclass Authentication {\n constructor(API_KEY) {\n this.isInitialized = false;\n this.endpoint = \"secure-stream-87726.herokuapp.com\";\n this.key = API_KEY;\n this.socket = new WebSocket(`wss://${this.endpoint}/qr`);\n this.init = this.init.bind(this);\n this.getQr = this.getQr.bind(this);\n this.destroy = this.destroy.bind(this);\n this.continousPing = this.continousPing.bind(this);\n }\n continousPing() {\n this.cPing = setInterval(() => {\n this.socket.send(\"ping\");\n }, 40000);\n }\n init(detailsHandler, ping = false) {\n this.isInitialized = true;\n this.shouldPing = ping;\n this.onRecieveUserDetails = this.onRecieveUserDetails.bind(this, detailsHandler);\n }\n destroy() {\n clearInterval(this.cPing);\n this.socket.close();\n }\n getQr() {\n let self = this;\n if (!self.isInitialized) {\n throw \"The module is not initialized; call init(...) before getQr()\";\n }\n return new Promise(function (resolve, reject) {\n self.socket.onopen = function () {\n self.socket.send(JSON.stringify({\n \"project_name\": \"Frontend-test\",\n \"api_key\": self.key,\n \"domain_name\": \"localhost\"\n }));\n if (self.shouldPing)\n self.continousPing();\n self.socket.onmessage = function (event) {\n let data = JSON.parse(event.data);\n if (data.ImageQR !== undefined) {\n resolve(data.ImageQR);\n }\n else if (data.message === \"success\") {\n self.onRecieveUserDetails(data);\n }\n };\n };\n });\n }\n onRecieveUserDetails(userDetailsHandler, data) {\n let self = this;\n userDetailsHandler(data.details);\n self.destroy();\n }\n}\nAuth = Authentication;\nexports.default = Authentication;\n", dependencies: [], sourceMap: {}, headerContent: undefined, mtime: 1532822399726, devLibsRequired : undefined, ac : undefined, _ : {} }