【问题标题】:Auth.currentSession says no current userAuth.currentSession 说没有当前用户
【发布时间】:2019-06-29 14:12:00
【问题描述】:

我有基本的反应应用程序,我正在尝试使用 aws-amplify 进行身份验证。 我在 cognito 中设置了一个用户池。我能够将用户发送到托管的 ui。用户可以登录,但是当我尝试获取当前会话时,我收到一条消息说no current user

这是我的代码。

import React, { Component } from 'react';
import './App.css';
import Amplify, { Auth } from 'aws-amplify';
import awsmobile from "./aws-exports";

const auth = {
  AppWebDomain: "funnybusty.auth.us-east-2.amazoncognito.com",
  TokenScopesArray: ["phone", "email", "profile", "openid", "aws.cognito.signin.user.admin"],
  RedirectUriSignIn: "http://localhost:3000",
  RedirectUriSignOut: "http://localhost:3000",
  responseType: "token",
  ClientId: "aaaaa",
  UserPoolId: "aaaa",
};

Amplify.configure(awsmobile);
Auth.configure({ oauth: auth });

class App extends Component {

  componentDidMount() {
    Auth.currentSession()
      .then(data => console.log(data))
      .catch(err => console.log(err));
  }

  click = () => {
    const config = Auth.configure().oauth
    const url = 'https://' + config.AppWebDomain + '/login?redirect_uri=' + config.RedirectUriSignIn + '&response_type=' + config.responseType + '&client_id=' + config.ClientId;
    window.location.assign(url);
  }

  render() {
    return (
      <div className="App">
        <button onClick={this.click}>
          Sign in with AWS
          </button>
      </div>
    );
  }
}

export default App;

我哪里错了?

【问题讨论】:

    标签: javascript reactjs amazon-cognito aws-amplify


    【解决方案1】:

    我从未使用过 reactjs,但我注意到了一些事情:

    1. 您必须使用Hub 模块 来监听signIn 事件。否则,您将无法取回您的用户信息。
    2. 在你的oauth配置中,你必须用http://localhost:3000/定义你的redirectSignInredirectSignOut(最后的/很重要,我学得很辛苦..)
    3. 您可能必须将 ClientIdUserPoolId 移出 const auth,因为 oauth 配置的模式是:

      const oauth = {
        domain: 'your_cognito_domain',
        scope: ['phone', 'email', 'profile', 'openid', 'aws.cognito.signin.user.admin'],
        redirectSignIn: 'http://localhost:3000/',
        redirectSignOut: 'http://localhost:3000/',
        responseType: 'code' // or token
      };
      

    有关更多信息,您可以查看文档中提供的完整示例:https://aws-amplify.github.io/docs/js/authentication#make-it-work-in-your-app

    【讨论】:

      【解决方案2】:

      原来这是我的代码中的一个细微错误。这一行,

      Auth.configure({ oauth: auth });
      

      应该是这样的。

      Auth.configure({
          Auth: {
            oauth: auth,
          }
        })
      

      文档对此不太清楚,因此我想将其留在这里以供未来的 google 人使用。

      当我看到 here 时,我一直在关注文档,但文档也提到了正确的方法 here

      我不确定这是否是文档中的某种错误,但确实令人困惑。希望这对其他人有所帮助。

      【讨论】:

        【解决方案3】:

        我知道这是一个旧线程,但我会为其他遇到此问题的人回答。

        我读到在componentDidMount() 中调用Auth.currentSession() 并不是一个好习惯。尝试在异步函数中调用它并将await 放在Auth.currentSession() 之前。

        例子:

        
        import { withAuthenticator } from 'aws-amplify-react'
        import Amplify, { Auth } from 'aws-amplify';
        Amplify.configure(aws_exports);
        
        class App extends Component {
          
            constructor(props) {
                super(props);
        
                this.state = {
                    postId: null
                };
            }
        
        async example() {
        
            
                let user = await Auth.currentSession().then(data => 
                console.log(data));
            
        
          }
        
        
        render() {
                return (
                  <div className="App">
                        <h5 className="card-header">Simple POST Request</h5>
                        <div className="card-body">
        
                        </div>
        
                        <button onClick={this.example}>
                    Req1!
                    </button>
        
                    </div>
        
        
                );
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2019-01-09
          • 2018-03-21
          • 2017-11-23
          • 2021-05-04
          • 2015-12-20
          • 2017-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多