Created
April 5, 2020 23:45
-
-
Save Stuyk/6b83b7c1d7ca6e93fece9cb168be7d2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as alt from 'alt'; | |
import * as path from 'path'; | |
import cors from 'cors'; | |
import http from 'http'; | |
import express from 'express'; | |
import chalk from 'chalk'; | |
import request from 'request'; | |
import { userIsVerified } from '../discord/bot'; | |
const htmlPath = path.join(alt.getResourcePath('life'), 'server/express/html'); | |
const styleSheets = path.join(alt.getResourcePath('life'), 'server/express/html/styles'); | |
//const oAuth = new discordOAuth2(); | |
const app = express(); | |
app.use(cors()); | |
app.use('/styles', express.static(styleSheets)); | |
app.get('/authenticate', async (req, res) => { | |
const codeToken = req.query.code; | |
const state = req.query.state; | |
if (!codeToken || !state) { | |
res.sendFile(path.join(htmlPath, '/fail.html'), err => {}); | |
return; | |
} | |
const options = { | |
method: 'POST', | |
url: 'https://discordapp.com/api/oauth2/token', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
}, | |
form: { | |
client_id: process.env['DISCORD_ID'], | |
client_secret: process.env['DISCORD_SECRET'], | |
grant_type: 'authorization_code', | |
code: codeToken, | |
scope: 'identify', | |
redirect_uri: `http://${process.env['OAUTH2_REDIRECT_IP']}:7790/authenticate` | |
} | |
}; | |
const discordResp = await new Promise(resolve => { | |
request(options, (err, response) => { | |
if (err) { | |
console.log(err); | |
return resolve(null); | |
} | |
resolve(JSON.parse(response.body)); | |
}); | |
}); | |
if (!discordResp.access_token) { | |
res.sendFile(path.join(htmlPath, '/fail.html'), err => {}); | |
return; | |
} | |
const bearerOptions = { | |
method: 'GET', | |
url: 'https://discordapp.com/api/users/@me', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
Authorization: `Bearer ${discordResp.access_token}` | |
}, | |
form: {} | |
}; | |
const userInfo = await new Promise(resolve => { | |
request(bearerOptions, (err, response) => { | |
if (err) { | |
return resolve(null); | |
} | |
let json; | |
try { | |
json = JSON.parse(response.body); | |
} catch (err) { | |
json = null; | |
} | |
resolve(json); | |
}); | |
}); | |
const guildOptions = { | |
method: 'GET', | |
url: 'https://discordapp.com/api/users/@me/guilds', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
Authorization: `Bearer ${discordResp.access_token}` | |
}, | |
form: {} | |
}; | |
if (!userInfo || !userInfo.id) { | |
res.sendFile(path.join(htmlPath, '/fail.html'), err => {}); | |
return; | |
} | |
const guildsInfo = await new Promise(resolve => { | |
request(guildOptions, (err, response) => { | |
if (err) { | |
return resolve(null); | |
} | |
let json; | |
try { | |
json = JSON.parse(response.body); | |
} catch (err) { | |
json = null; | |
} | |
resolve(json); | |
}); | |
}); | |
if (guildsInfo) { | |
const guildIndex = guildsInfo.findIndex(guild => { | |
if (guild.id === `251381024917946371`) { | |
return guild; | |
} | |
}); | |
if (guildIndex <= -1) { | |
const guildJoinOptions = { | |
method: 'PUT', | |
url: `https://discordapp.com/api/guilds/251381024917946371/members/${userInfo.id}`, | |
headers: { | |
'Content-Type': 'application/json', | |
Authorization: `Bot ${process.env[`DISCORD_TOKEN`]}` | |
}, | |
json: { | |
access_token: discordResp.access_token, | |
roles: [`${process.env['DISCORD_ROLE_VERIFIED']}`] | |
} | |
}; | |
await new Promise(resolve => { | |
request(guildJoinOptions, (err, response) => { | |
if (err) { | |
return resolve(null); | |
} | |
let json; | |
try { | |
json = JSON.parse(response.body); | |
console.log(response.body); | |
} catch (err) { | |
json = null; | |
} | |
resolve(json); | |
}); | |
}); | |
alt.emit( | |
'discord:Gamelog', | |
`ID: ${userInfo.id} | Name: ${userInfo.username}#${userInfo.discriminator} was added to Discord.` | |
); | |
} | |
} | |
const player = alt.Player.all.find(player => { | |
if (player.token === state) { | |
return player; | |
} | |
}); | |
if (!player) { | |
res.sendFile(path.join(htmlPath, '/fail.html'), err => {}); | |
return; | |
} | |
const discordID = userInfo.id; | |
const isVerified = await userIsVerified(discordID); | |
if (!isVerified) { | |
alt.emitClient(player, 'discord:VerifyError'); | |
res.sendFile(path.join(htmlPath, '/fail.html'), err => {}); | |
return; | |
} | |
player.token = null; | |
delete player.token; | |
alt.emit('discord:FinishLogin', player, discordID); | |
res.sendFile(path.join(htmlPath, '/done.html'), err => {}); | |
}); | |
const httpPort = parseInt(process.env['OAUTH2_PORT']); | |
http.createServer(app).listen(httpPort, () => { | |
alt.log(chalk.cyanBright(`OAUTH2: server.altv.life:${httpPort}`)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment