Estoy escribiendo un código de inicio de sesión del lado del servidor para aws cognito y quiero verificar que el usuario que está iniciando sesión existe en el grupo de identidades y obtener los atributos que se le asignan.
Para el inicio de sesión por correo electrónico, esto funciona bien usando el siguiente código, usando 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) }Pero estoy atascado tratando de hacer algo similar para los usuarios federados (iniciar sesión a través de Google, por ejemplo). ¿Alguien me puede ayudar?
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)) }); } };Tengo un punto final de inicio de sesión. trata eso.