Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

246
Visualizações
node.js server-side validation of cognito credentials

I am writing some server side login code for aws cognito and I want to verify the user who is logging in exists in the identity pool and to get the attributes assigned to them.

For email login I have this working well using the following code - using the aws-sdk:

let cognitoVerifyUser = null
            try {
                const cognitoIdProvider = new AWS.CognitoIdentityServiceProvider()
                cognitoVerifyUser = await cognitoIdProvider.adminGetUser({
                    UserPoolId: pool.userPoolId,
                    Username: username,
                }).promise()
            } catch (e) { 
                throwError(e, e.message)
            }

            if (!cognitoVerifyUser) {
                throwError(error.unauthorized, e)
            }

            const emailAttrib = cognitoVerifyUser.UserAttributes.find(a => a.Name == 'email')
            if (!cognitoVerifyUser.Enabled || cognitoVerifyUser.UserStatus != 'CONFIRMED' || username != cognitoVerifyUser.Username || email != emailAttrib.Value) {
                throwError(error.unauthorized, e)
            }

But I am stuck trying to do something similar for federated users (login via google for example). Can someone help me out?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

import generateResponse from "../../../Utils/generateResponse";
import {
  CognitoUserPool,
  CognitoUser,
  AuthenticationDetails
} from "amazon-cognito-identity-js";
import { APIGatewayEvent } from "aws-lambda";

type LoginType = {
  email: string;
  password: string;
};

export const handler = async (event: APIGatewayEvent) => {
  try {
    const body = JSON.parse(event.body as string) as LoginType;

    const userPool = new CognitoUserPool({
      UserPoolId: process.env.COGNITO_USERPOOLID as string,
      ClientId: process.env.COGNITO_CLIENTID as string
    });

    const user = new CognitoUser({ Username: body.email, Pool: userPool });

    const authenticationData = {
      Username: body.email,
      Password: body.password
    };
    const authenticationDetails = new AuthenticationDetails(authenticationData);

    return new Promise(resolve =>
      user.authenticateUser(authenticationDetails, {
        //@ts-ignore
        onSuccess: result => {
          resolve({ body: JSON.stringify(result) });
        },
        onFailure: err => {
          resolve({ body: JSON.stringify(err) });
        }
      })
    );
  } catch (err) {
    return generateResponse({
      statusCode: 400,
      body: JSON.stringify(err, Object.getOwnPropertyNames(err))
    });
  }
};

i have a login endpoint. try that.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda