|  | @@ -1,11 +1,12 @@
 | 
	
		
			
				|  |  |  import { Request as ExpressRequest, Response as ExpressResponse } from "express";
 | 
	
		
			
				|  |  | -import { OAuth2 } from "src/utils/util";
 | 
	
		
			
				|  |  | +import { DiscordAPI } from "src/utils/util";
 | 
	
		
			
				|  |  |  import { ENVIRONMENT } from "src/utils/environment";
 | 
	
		
			
				|  |  | -import { Option } from "@shared/common/async_utils";
 | 
	
		
			
				|  |  | +import { Option, tryDo } from "@shared/common/async_utils";
 | 
	
		
			
				|  |  |  import { logger } from "src/utils/logging";
 | 
	
		
			
				|  |  | +import { rpcClient } from "src/utils/rpc";
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  export const get = async (req: ExpressRequest, res: ExpressResponse): Promise<void> => {
 | 
	
		
			
				|  |  | -    res.redirect(OAuth2.getAuthUrl({
 | 
	
		
			
				|  |  | +    res.redirect(DiscordAPI.getAuthUrl({
 | 
	
		
			
				|  |  |          client_id: ENVIRONMENT.clientId,
 | 
	
		
			
				|  |  |          redirect_url: ENVIRONMENT.redirectUrl,
 | 
	
		
			
				|  |  |          response_type: "code",
 | 
	
	
		
			
				|  | @@ -31,7 +32,7 @@ export const post = async (req: ExpressRequest, res: ExpressResponse):
 | 
	
		
			
				|  |  |              error: "Authentication token is missing. Please try logging in again.",
 | 
	
		
			
				|  |  |          });
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    const result = await OAuth2.getToken({
 | 
	
		
			
				|  |  | +    const tokenResult = await DiscordAPI.getToken({
 | 
	
		
			
				|  |  |          client_id: ENVIRONMENT.clientId,
 | 
	
		
			
				|  |  |          client_secret: ENVIRONMENT.clientSecret,
 | 
	
		
			
				|  |  |          grant_type: "authorization_code",
 | 
	
	
		
			
				|  | @@ -39,10 +40,25 @@ export const post = async (req: ExpressRequest, res: ExpressResponse):
 | 
	
		
			
				|  |  |          scope: "identify",
 | 
	
		
			
				|  |  |          redirect_uri: ENVIRONMENT.redirectUrl,
 | 
	
		
			
				|  |  |      });
 | 
	
		
			
				|  |  | -    if (!result.ok) {
 | 
	
		
			
				|  |  | -        return res.json(result);
 | 
	
		
			
				|  |  | +    if (!tokenResult.ok) {
 | 
	
		
			
				|  |  | +        return res.json(tokenResult);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | -    req.sessionOptions.maxAge = result.expires_in;
 | 
	
		
			
				|  |  | +    const userResult = await DiscordAPI.getCurrentUser(tokenResult.access_token);
 | 
	
		
			
				|  |  | +    if (!userResult.ok) {
 | 
	
		
			
				|  |  | +        return res.json(userResult);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    const userInServerResult = await tryDo(rpcClient.userInServer({ userId: userResult.id }));
 | 
	
		
			
				|  |  | +    if (!userInServerResult.ok) {
 | 
	
		
			
				|  |  | +        logger.error("WEB: failed to auth user %s: %s", userResult.id, userInServerResult.error);
 | 
	
		
			
				|  |  | +        return res.json({ ok: false, error: "Couldn't determine if user joined the server, please try again later" });
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    if (!userInServerResult.result.exists) {
 | 
	
		
			
				|  |  | +        return res.json({ ok: false, error: "You haven't joined any servers NoctBot manages! Please join first and try again!" });
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    req.session.authTokenCode = undefined;
 | 
	
		
			
				|  |  | +    req.session.userId = userResult.id;
 | 
	
		
			
				|  |  | +    req.session.username = `${userResult.username}#${userResult.discriminator}`;
 | 
	
		
			
				|  |  | +    req.sessionOptions.maxAge = tokenResult.expires_in;
 | 
	
		
			
				|  |  |      return res.json({
 | 
	
		
			
				|  |  |          ok: true,
 | 
	
		
			
				|  |  |      });
 |