mirror of
https://github.com/danbulant/redditConnector
synced 2026-07-05 11:01:00 +00:00
Implement login
This commit is contained in:
parent
47e3c500e1
commit
78d0cdf5e0
2 changed files with 52 additions and 4 deletions
55
index.js
55
index.js
|
|
@ -1,28 +1,75 @@
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
|
const cookieParser = require('cookie-parser')
|
||||||
const discord = require("discord.js");
|
const discord = require("discord.js");
|
||||||
const Auth = require("discord-oauth2");
|
const Auth = require("discord-oauth2");
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const config = require("./config.json");
|
const config = require("./config.json");
|
||||||
|
|
||||||
const client = new discord.Client();
|
const client = new discord.Client({
|
||||||
|
ws: {
|
||||||
|
intents: [
|
||||||
|
'GUILDS'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
});
|
||||||
const app = express();
|
const app = express();
|
||||||
const auth = new Auth({
|
const auth = new Auth({
|
||||||
clientId: config.cid,
|
clientId: config.cid,
|
||||||
clientSecret: config.secret
|
clientSecret: config.secret
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.use(cookieParser());
|
||||||
|
|
||||||
app.get("/login", (req, res) => {
|
app.get("/login", (req, res) => {
|
||||||
var state = crypto.randomBytes(64).toString('hex');
|
var state = crypto.randomBytes(64).toString('hex');
|
||||||
res.cookie("state", state);
|
res.cookie("state", state);
|
||||||
res.redirect(auth.generateAuthUrl({
|
res.redirect(auth.generateAuthUrl({
|
||||||
scope: ["connections", "identify"],
|
scope: ["connections", "identify"],
|
||||||
state
|
state,
|
||||||
|
redirectUri: "http://localhost:8080/callback"
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/callback", (req, res) => {
|
app.get("/callback", async (req, res) => {
|
||||||
|
const { code, state } = req.query;
|
||||||
|
if(state !== req.cookies.state) return res.status(400).send("Wrong state");
|
||||||
|
try {
|
||||||
|
var token = await auth.tokenRequest({
|
||||||
|
code,
|
||||||
|
scope: [ "connections", "identify" ],
|
||||||
|
grantType: "authorization_code",
|
||||||
|
redirectUri: "http://localhost:8080/callback"
|
||||||
|
});
|
||||||
|
} catch(e) {
|
||||||
|
return res.status(403).send("Invalid code");
|
||||||
|
}
|
||||||
|
var connections = await auth.getUserConnections(token.access_token);
|
||||||
|
var reddit = connections.filter(val => val.type === "reddit");
|
||||||
|
if(!reddit) return res.send("No reddit connection");
|
||||||
|
reddit = reddit[0];
|
||||||
|
var user = await auth.getUser(token.access_token);
|
||||||
|
|
||||||
|
try {
|
||||||
|
var guild = await client.guilds.fetch(config.guild);
|
||||||
|
} catch(e) {
|
||||||
|
return res.status(500).send("Guild not found");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
var member = await guild.members.fetch(user.id);
|
||||||
|
} catch(e) {
|
||||||
|
return res.status(403).send("Member not found");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await member.setNickname("u/" + reddit.name);
|
||||||
|
} catch(e) {
|
||||||
|
console.log(e);
|
||||||
|
return res.status(500).send("Missing nickname permission");
|
||||||
|
}
|
||||||
|
res.send("Done");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.login(config.token);
|
||||||
|
|
||||||
client.login(config.token);
|
app.listen(8080, () => {
|
||||||
|
console.log("http://localhost:8080/login");
|
||||||
|
})
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"cookie-parser": "^1.4.5",
|
||||||
"discord-oauth2": "^2.3.0",
|
"discord-oauth2": "^2.3.0",
|
||||||
"discord.js": "^12.3.1",
|
"discord.js": "^12.3.1",
|
||||||
"express": "^4.17.1"
|
"express": "^4.17.1"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue