我正在遵循使用Cognito和React客户端进行用户身份验证的基本设置。登录成功并返回用户信息,但是Auth.currentSession()
返回No current user
。
登录组件:
import React, { Component } from 'react'
import { Auth } from 'aws-amplify'
class Login extends Component {
// ... other stuff
handleUser = async () => {
try {
let user = await Auth.currentSession()
console.log(user) // <=== "No current user"
} catch (e) {
console.log(e)
}
}
handleSubmit = async event => {
event.preventDefault()
try {
let user = await Auth.signIn(this.state.email, this.state.password)
console.log(user) // <=== works fine, returns "CognitoUser {username:...
} catch (e) {
console.log(e.message)
}
}
render () {
return (
<div>
<Form onSubmit={this.handleSubmit}>
// form
</Form>
<Button onClick={this.handleUser}>
USER
</Button>
</div>
)
}
}
index.js
Amplify.configure({
Auth: {
region: config.cognito.REGION,
userPoolId: config.cognito.USER_POOL_ID,
userPoolWebClientId: config.cognito.APP_CLIENT_ID,
identityPoolId: config.cognito.IDENTITY_POOL_ID
}
})