#How to Add Discord Login to Cognito User Pool

2 messages · Page 1 of 1 (latest)

winged junco
#

I want to OpenID connect with idp as discord using cognito.
tried setting the setup method to Auto fill through issuer URL, but an error occurred. I searched and found a third party wrapper.

Is it necessary to create a wrapper for Discord?

mild citrus
#

👋 yes it will be necessary to create a wrapper, but I believe only a few pieces are needed. Cognito provides a way of specifying the URLs manually for tokens, authorization, and user info calls. For the most part you can use the /@me route from Discord's API for userinfo and specify the appropriate attribute mapping, and just wrap the authorization call.

import type { APIGatewayProxyHandlerV2 } from "aws-lambda"
import { OAuth2Routes } from "discord-api-types/rest/v10"

/**
 * Cognito forces OIDC providers to request the "openid" scope
 * This does not exist in Discord, and therefore we need to remove it from the request
 * It shouldn't harm anything, but works around the Cognito constraint
 */
export const handler: APIGatewayProxyHandlerV2 = async (event) => {
  const params = new URLSearchParams(event.rawQueryString)
  // this should look something like "openid email identity"
  const scope = params.get("scope")
  if (!scope) {
    return {
      statusCode: 500,
    }
  }

  const purged = scope
    .split(" ")
    .filter((s) => s !== "openid")
    .join(" ")
  params.set("scope", purged)

  // const discord = new Discord(params.get)

  return {
    statusCode: 302,
    headers: {
      Location: `${OAuth2Routes.authorizationURL}?${params.toString()}`,
    },
  }
}