【发布时间】:2021-01-08 10:27:59
【问题描述】:
我正在尝试在 cognito 上创建一个带有 react 的演示应用程序。 为了尝试一下,我正在尝试这个git_repo中的代码
但我收到错误 Username and Pool information required 。我不知道为什么。
我设置了一个用户池和客户端 ID,并在代码中使用它们。
在创建应用程序客户端时,我还取消选中框 Genereate client secret。
错误指向的行是 props.setSession(session);
import {
AuthenticationDetails,
CognitoUser,
CognitoUserSession,
} from "amazon-cognito-identity-js";
import React, { useState } from "react";
import { useForm } from "react-hook-form";
import { useHistory } from "react-router-dom";
import userPool from "../aws/UserPool";
type FormData = {
email: string;
password: string;
};
type LoginProps = {
setSession: React.Dispatch<React.SetStateAction<CognitoUserSession | null>>;
};
const Login = (props: LoginProps) => {
const { register, handleSubmit, errors } = useForm<FormData>();
const history = useHistory();
const [errorMessage, setErrorMessage] = useState("");
const onSubmit = handleSubmit((values) => {
const user = new CognitoUser({
Username: values.email,
Pool: userPool,
});
const authenticationDetails = new AuthenticationDetails({
Username: values.email,
Password: values.password,
});
user.authenticateUser(authenticationDetails, {
onSuccess: (session) => {
console.log("Login successful");
props.setSession(session);
history.push("/");
},
onFailure: (err) => {
console.error("Login failed", err);
setErrorMessage(`An error occurred while logging in: ${err?.message}`);
},
});
});
return (
<div>
<p>
Welcome to this sample application <br /> Login below.
</p>
<form onSubmit={onSubmit} className="form">
<input name="email" placeholder="email" type="email" ref={register} />
{errors.email && errors.email.message}
<input
name="password"
placeholder="password"
type="password"
ref={register}
/>
{errors.password && errors.password.message}
<button type="submit">Login</button>
{errorMessage}
</form>
</div>
);
};
export default Login;
谁能帮我解决我在这里遗漏的任何步骤?
【问题讨论】:
-
这个问题你解决了吗?
标签: reactjs amazon-web-services session amazon-cognito