file description module import type { User } from "eris" ; import type { StrategyOptions } from "passport-discord" ; import { Strategy } from "passport-discord" ; import { destroySession } from "../../utils/webserver" ; import express from "express" ; import rateLimit from "express-rate-limit" ; import passport from "passport" ; import config from "../../../config.json" ; const scope = [ "identify" , "guilds" ] ; const router = express . Router ( ) ; const authRateLimit = rateLimit ( { windowMs : 1 * 60 * 1000 , max : 15 , message : "Too many attempts to authenticate or deauthenticate in a short period of time. Try again later" , } ) ; export function authRoutes ( user ) { const strategyOptions = { clientID : user . id , clientSecret : config . dashboard . botSecret , callbackURL : config . dashboard . redirectURI , scope : scope , } ; passport . use ( new Strategy ( strategyOptions , ( _accessToken , _refreshToken , profile , done ) => { process . nextTick ( ( ) => { return done ( null , profile ) ; } ) ; } ) ) ; router . get ( "/" , passport . authenticate ( "discord" , { scope : scope } ) ) ; router . get ( "/callback/" , authRateLimit , passport . authenticate ( "discord" , { failureRedirect : "/auth/fail/" , successRedirect : "/manage/servers/" } ) ) ; router . get ( "/logout/" , authRateLimit , async ( req , res ) => { await destroySession ( req ) . then ( ( ) => { res . redirect ( 301 , "/" ) ; } ) . catch ( ( ) => { res . status ( 500 ) . send ( { error : "Failed to destroy the session data. Try clearing your cookies manually." } ) ; } ) ; } ) ; router . get ( "/fail/" , authRateLimit , async ( req , res ) => { await destroySession ( req ) . then ( ( ) => { res . render ( "authfail" ) ; } ) ; } ) ; router . get ( "/ratelimited/" , async ( req , res ) => { await destroySession ( req ) . then ( ( ) => { res . render ( "ratelimited" ) ; } ) ; } ) ; passport . serializeUser ( ( user , done ) => done ( null , user ) ) ; passport . deserializeUser ( ( user , done ) => done ( null , user ) ) ; return router ; }