Cognito Auth.currentSession返回成功登录后没有当前用户

时间:2019-06-15 15:59:02

标签: javascript reactjs amazon-web-services authentication amazon-cognito

我正在遵循使用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
  }
})

0 个答案:

没有答案