// const http = require('http') // const fs = require('fs') // const {addSkirdaAccount} = require('configmanager') // const auth_api_url = 'http://192.168.88.10:8083' const auth_api_url = 'http://skirda-auth.brzezinski.ru' class SkirdaDiscordAuth{ /** * @typedef {Object} DiscordRedirectAuth * @property {string} redirectUrl how the person is called * @property {string} requestId how many years the person lived * * @return {Promise} * */ temp_installId = '26c175e2-71bd-4d80-9553-cc1575fc8ef9' async Init(){ const resp = await fetch(`${auth_api_url}/v1/auth/discord/init?installId=${this.temp_installId}`) if (resp.statusCode !== 200){ return await resp.text() } return await resp.json() } /** * @param requestId {string} Request id from init route // * @param timeout {number} Timeout to poll in seconds * @return {SkirdaUserResp | string} Result of OAuth login */ async CyclePolling(requestId){ for (let i = 0; i < 15; i++) { const resp = await this._poll(requestId) // console.log(resp) switch (resp.status){ case 204: await this._sleep(4000) continue case 200: return await resp.json() default: console.error(await resp.text()) return 'TODO error' } } } /** * @typedef {Object} SkirdaUserResp * @property skirdaUserId {string} * @property username {string} * @property token {string} * * @param requestId {string} * @return {Response} * */ async _poll(requestId){ const resp = await fetch(`${auth_api_url}/v1/auth/discord/periodicPolling?installId=${this.temp_installId}&requestId=${requestId}`) return resp } _sleepSetTimeout_ctrl /** * @param ms {number} * @return {boolean} */ async _sleep(ms) { clearInterval(this._sleepSetTimeout_ctrl) return new Promise(resolve => this._sleepSetTimeout_ctrl = setTimeout(resolve, ms)) } } const auth = new SkirdaDiscordAuth() const loginSkirdaDiscordButton = document.getElementById('loginSkirdaDiscordInitAuth') loginSkirdaDiscordButton.addEventListener('click', async () =>{ const res = await auth.Init() const redir = JSON.parse(res) //FIXME console.log(redir) const resOpenUrl = await shell.openExternal(redir.redirectUrl) const skirdaAuth = await auth.CyclePolling(redir.requestId) // console.log(skirdaAuth) // skirdaAuth.skirdaUserId = '2a5fd868-1ac5-4ccf-a22f-183822de2d61' let account = { uuid: '2a5fd868-1ac5-4ccf-a22f-183822de2d61', token: '', username: skirdaAuth.skirdaUserId, displayName: skirdaAuth.username } AuthManager.addSkirdaAccount(account.uuid, account.token, account.username, account.displayName) setTimeout(() => { switchView(VIEWS.loginSkirdaDiscord, VIEWS.landing, 500, 500) }, 1000) })