/* global settings,twitch, fs, settingsPath, ini, shell, options, axios */ const twitchAuthentication = () => new Promise(resolve => { const http = require('http'); const redirectUri = 'http://localhost:1989/auth'; const scopes = ['chat:edit', 'chat:read', 'user:read:follows', 'user:read:subscriptions']; const express = require('express'); const tempAuthServer = express(); const port = 1989; const { parse: parseQueryString } = require('querystring'); tempAuthServer.use(function (req, res, next) { if (req.url !== '/auth') { const token = parseQueryString(req.query.auth); settings.TWITCH.OAUTH_TOKEN = token['#access_token']; fs.writeFileSync(settingsPath, ini.stringify(settings)); resolve(token['#access_token']); stopServer(); } next(); }); const htmlString = ` Authentication

Authentication successful! You can close this window now.

`; tempAuthServer.get('/auth', (req, res) => { res.send(htmlString); }); tempAuthServer.post('/auth', (req, res) => { res.render('authentication', { name: req.body.name }); }); const server = http.createServer(tempAuthServer); server.listen(port, () => { const authURL = `https://id.twitch.tv/oauth2/authorize?client_id=${settings.TWITCH.CLIENT_ID}&redirect_uri=${encodeURIComponent( redirectUri )}&response_type=token&scope=${scopes.join(' ')}`; shell.openExternal(authURL); }); function stopServer() { server.close(() => {}); } }); function getTwitchOauthToken() { return twitchAuthentication().then(res => { twitch.getTwitchUserId(); // twitch.getTwitchChannelId(); return res; }); } module.exports = { getTwitchOauthToken };