|
@@ -1,24 +1,71 @@
|
|
import { Request, Response, NextFunction } from "express";
|
|
import { Request, Response, NextFunction } from "express";
|
|
import request from "request-promise-native";
|
|
import request from "request-promise-native";
|
|
import { Response as Res } from "request";
|
|
import { Response as Res } from "request";
|
|
|
|
+import { botService } from "src/util/rpc_client";
|
|
|
|
|
|
-export async function get(req : Request, res : Response, next: NextFunction) {
|
|
|
|
- const CALLBACK_URL = encodeURIComponent(`${process.env.ADMIN_URL}/login/discord/callback`);
|
|
|
|
|
|
+const API_ENDPOINT = "https://discordapp.com/api";
|
|
|
|
|
|
|
|
+export async function get(req : Request, res : Response, next: NextFunction) {
|
|
if(!req.query.code)
|
|
if(!req.query.code)
|
|
throw new Error("NoCodeProvided");
|
|
throw new Error("NoCodeProvided");
|
|
|
|
|
|
let code = req.query.code;
|
|
let code = req.query.code;
|
|
- let response = await request(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${CALLBACK_URL}`, {
|
|
|
|
|
|
+ let response = await request("/oauth2/token", {
|
|
method: "POST",
|
|
method: "POST",
|
|
|
|
+ baseUrl: API_ENDPOINT,
|
|
|
|
+ qs: {
|
|
|
|
+ grant_type: "authorization_code",
|
|
|
|
+ code: code,
|
|
|
|
+ redirect_uri: `${process.env.ADMIN_URL}/login/discord/callback`
|
|
|
|
+ },
|
|
auth: {
|
|
auth: {
|
|
user: process.env.BOT_CLIENT_ID,
|
|
user: process.env.BOT_CLIENT_ID,
|
|
- pass: process.env.BOT_CLIENT_SECRET,
|
|
|
|
- sendImmediately: true
|
|
|
|
|
|
+ pass: process.env.BOT_CLIENT_SECRET
|
|
},
|
|
},
|
|
resolveWithFullResponse: true
|
|
resolveWithFullResponse: true
|
|
}) as Res;
|
|
}) as Res;
|
|
|
|
|
|
- //TODO: Saving the tokens with horses and stuff
|
|
|
|
- console.log(response.toJSON());
|
|
|
|
-};
|
|
|
|
|
|
+ let authResponse: AuthResponse = JSON.parse(response.body);
|
|
|
|
+
|
|
|
|
+ let userInfoResponse = await request("/users/@me", {
|
|
|
|
+ method: "GET",
|
|
|
|
+ baseUrl: API_ENDPOINT,
|
|
|
|
+ auth: {
|
|
|
|
+ bearer: authResponse.access_token
|
|
|
|
+ },
|
|
|
|
+ resolveWithFullResponse: true
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ let discordUser : DiscordUser = JSON.parse(userInfoResponse.body);
|
|
|
|
+ let userInfo = await botService.getModeratorUserInfo({id: discordUser.id});
|
|
|
|
+
|
|
|
|
+ if(!userInfo){
|
|
|
|
+ res.redirect(`${process.env.ADMIN_URL}/login/?error=invalid_user`);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ req.session.user = userInfo;
|
|
|
|
+ res.redirect(`${process.env.ADMIN_URL}/`);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+interface AuthResponse {
|
|
|
|
+ access_token: string;
|
|
|
|
+ token_type: string;
|
|
|
|
+ expires_in: number;
|
|
|
|
+ refresh_token?: string;
|
|
|
|
+ scope: string;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+interface DiscordUser {
|
|
|
|
+ id: string;
|
|
|
|
+ username: string;
|
|
|
|
+ discriminator: string;
|
|
|
|
+ avatar?: string;
|
|
|
|
+ bot?: boolean;
|
|
|
|
+ mfa_enabled?: boolean;
|
|
|
|
+ locale?: string;
|
|
|
|
+ verified?: boolean;
|
|
|
|
+ email?: string;
|
|
|
|
+ flags?: number;
|
|
|
|
+ premium_type?: number;
|
|
|
|
+}
|