converted discord linking to mongo

This commit is contained in:
Miodec 2021-07-09 22:17:19 +01:00
parent eb5137e11c
commit bec7f380ca
10 changed files with 89 additions and 25 deletions

View file

@ -4,6 +4,8 @@ const {
isUsernameValid,
isTagPresetNameValid,
} = require("../../handlers/validation");
const MonkeyError = require("../../handlers/error");
const fetch = require("node-fetch");
// import UsersDAO from "../../dao/user";
// import BotDAO from "../../dao/bot";
@ -78,11 +80,18 @@ class UserController {
const { uid } = req.decodedToken;
let discordFetch = await fetch("https://discord.com/api/users/@me", {
headers: {
authorization: `${req.tokenType} ${req.accessToken}`,
authorization: `${req.body.data.tokenType} ${req.body.data.accessToken}`,
},
});
discordFetch = await discordFetch.json();
const did = discordFetch.id;
if (!did) {
throw new MonkeyError(
500,
"Could not get Discord account info",
"did is undefined"
);
}
let user;
try {
user = await UsersDAO.getUserByDiscordId(did);
@ -90,13 +99,27 @@ class UserController {
user = null;
}
if (user !== null) {
return next(
throw new MonkeyError(
400,
"This Discord account is already linked to a different account"
);
}
await UsersDAO.linkDiscord(uid, did);
await BotDAO.linkDiscord(uid, did);
return res.sendStatus(200);
return res.status(200).json({
message: "Discord account linked",
did,
});
} catch (e) {
return next(e);
}
}
static async unlinkDiscord(req, res, next) {
try {
const { uid } = req.decodedToken;
await UsersDAO.unlinkDiscord(uid);
return res.status(200).send();
} catch (e) {
return next(e);
}

View file

@ -24,4 +24,12 @@ router.post("/tags/remove", authenticateRequest, UserController.removeTag);
router.post("/tags/edit", authenticateRequest, UserController.editTag);
router.post("/discord/link", authenticateRequest, UserController.linkDiscord);
router.post(
"/discord/unlink",
authenticateRequest,
UserController.unlinkDiscord
);
module.exports = router;

5
package-lock.json generated
View file

@ -21,6 +21,7 @@
"howler": "^2.2.1",
"mongodb": "^3.6.9",
"mongoose": "^5.12.12",
"node-fetch": "^2.6.1",
"nodemon": "^2.0.7",
"path": "^0.12.7",
"tinycolor2": "^1.4.2",
@ -10242,7 +10243,6 @@
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==",
"optional": true,
"engines": {
"node": "4.x || >=6.0.0"
}
@ -22473,8 +22473,7 @@
"node-fetch": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==",
"optional": true
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw=="
},
"node-forge": {
"version": "0.10.0",

View file

@ -59,6 +59,7 @@
"howler": "^2.2.1",
"mongodb": "^3.6.9",
"mongoose": "^5.12.12",
"node-fetch": "^2.6.1",
"nodemon": "^2.0.7",
"path": "^0.12.7",
"tinycolor2": "^1.4.2",

View file

@ -2,27 +2,32 @@ import * as Notifications from "./notifications";
import * as Settings from "./settings";
import * as DB from "./db";
import axiosInstance from "./axios-instance";
import * as Loader from "./loader";
export let data = null;
export function set(val) {
data = val;
}
export function verify(user) {
Notifications.add("Verifying", 0, 3);
export async function verify(user) {
Notifications.add("Linking Discord account", 0, 3);
Loader.show();
data.uid = user.uid;
axiosInstance
.post("/verifyDiscord", {
data: data,
})
.then((response) => {
if (response.data.status === 1) {
Notifications.add(response.data.message, 1);
DB.getSnapshot().discordId = response.data.did;
Settings.updateDiscordSection();
} else {
Notifications.add(response.data.message, -1);
}
});
let response;
try {
response = await axiosInstance.post("/user/discord/link", { data: data });
} catch (e) {
Loader.hide();
let msg = e?.response?.data?.message ?? e.message;
Notifications.add("Failed to link Discord: " + msg, -1);
return;
}
Loader.hide();
if (response.status !== 200) {
Notifications.add(response.data.message);
} else {
Notifications.add("Accounts linked", 1);
DB.getSnapshot().discordId = response.data.did;
Settings.updateDiscordSection();
}
}

View file

@ -63,6 +63,7 @@ export async function initSnapshot() {
snap.personalBests = userData.personalBests;
snap.banned = userData.banned;
snap.verified = userData.verified;
snap.discordId = userData.discordId;
snap.globalStats = {
time: userData.timeTyping,
started: userData.startedTests,

View file

@ -46,6 +46,8 @@ $(document).ready(() => {
});
history.replaceState("/", null, "/");
}
let page = window.location.pathname.replace("/", "");
UI.changePage(page);
} else if (window.location.pathname === "/account") {
// history.replaceState("/", null, "/");
} else if (/challenge_.+/g.test(window.location.pathname)) {

View file

@ -8,7 +8,7 @@ let mappedRoutes = {
"/settings": "pageSettings",
"/about": "pageAbout",
"/account": "pageAccount",
"/verify": "pageTest",
"/verify": "pageLoading",
};
export function handleInitialPageClasses(pathname) {

View file

@ -366,8 +366,26 @@ list.unlinkDiscord = new SimplePopup(
[],
"Are you sure you want to unlink your Discord account?",
"Unlink",
() => {
async () => {
Loader.show();
let response;
try {
response = await axiosInstance.post("/user/discord/unlink", {});
} catch (e) {
Loader.hide();
let msg = e?.response?.data?.message ?? e.message;
Notifications.add("Failed to unlink Discord: " + msg, -1);
return;
}
Loader.hide();
if (response.status !== 200) {
Notifications.add(response.data.message);
} else {
Notifications.add("Accounts unlinked", 1);
DB.getSnapshot().discordId = undefined;
Settings.updateDiscordSection();
}
//todo rewrite to axios
// CloudFunctions.unlinkDiscord({
// uid: firebase.auth().currentUser.uid,

View file

@ -2030,12 +2030,19 @@
might result in the bot not being able to give you a role.
</div>
<div class="buttons">
<a
<!-- <a
class="button"
href="https://discord.com/api/oauth2/authorize?client_id=798272335035498557&redirect_uri=https%3A%2F%2Fmonkeytype.com%2Fverify&response_type=token&scope=identify"
style="text-decoration: none"
>
Link with Discord
</a> -->
<a
class="button"
href="https://discord.com/api/oauth2/authorize?client_id=798272335035498557&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fverify&response_type=token&scope=identify"
style="text-decoration: none"
>
Link with Discord
</a>
</div>
<div class="info hidden">