在服务器端调用 Amplify.Auth.signIn() 方法时,在登录前需检查用户输入的用户名和密码是否正确。如果用户的凭证输入错误,登录过程会失败并返回 signInUserSession:null。
try {
const user = await Amplify.Auth.signIn(username, password);
console.log(user);
} catch (error) {
console.error(error);
}
如果用户凭证输入无误但仍然返回 signInUserSession:null,可以调试代码以获取更多信息。在 AWS 控制台中查看 CloudWatch Logs 或在本地使用调试工具(如 VSCode)以获取更多信息。
try {
const user = await Amplify.Auth.signIn(username, password);
console.log(user);
} catch (error) {
console.error(error);
if (error.code === 'NotAuthorizedException') {
console.log('Invalid username or password');
}
// Add more debug information here
}
在 Amplify Auth 中,当用户进行身份验证后,返回的 signInUserSession 对象中会包含用户的 ID 令牌(ID token)。如果 signInUserSession 为 null,则表示用户尚未进行身份验证或未经过身份验证。
const user = await Auth.signIn(username, password);
if (user.signInUserSession != null) {
// User is authenticated
} else {
console.log('User is not authenticated');
}
以上是几种可能的解决方案,如果问题仍未解决,需要进一步调试和排查。