简体   繁体   English

aws-放大-反应。 身份验证器组件-退出按钮

[英]aws-amplify-react . Authenticator Component - Signout Button

Following 以下

https://aws-amplify.github.io/docs/js/authentication#using-the-authenticator-component-directly https://aws-amplify.github.io/docs/js/authentication#using-the-authenticator-component-directly

import { Authenticator} from 'aws-amplify-react'
import Amplify from 'aws-amplify';
import aws_exports from './aws_exports';


Amplify.configure(aws_exports);


const AppWithAuth = () => (
    <Authenticator>
            <App/>
    </Authenticator>
)

export default AppWithAuth

I am trying to use the Authenicator component directly. 我正在尝试直接使用Authenicator组件。 How do I display a signout button once I am logged in. 登录后如何显示退出按钮。

Tried following https://github.com/richardzcode/Journal-AWS-Amplify-Tutorial/tree/master/step-02#sign-out-button 尝试以下https://github.com/richardzcode/Journal-AWS-Amplify-Tutorial/tree/master/step-02#sign-out-button

import { Authenticator , SignOut} from 'aws-amplify-react'

const AppWithAuth = () => (
    <Authenticator>
        <SignOut />
            <App/>
    </Authenticator>
)

But Signout button is still not visible 但是“退出”按钮仍然不可见

It could be because SignOut button is outside App . 这可能是因为“ SignOut按钮在App外部。 It probably is rendered but just not visible because of CSS layout. 它可能已呈现,但由于CSS布局而不可见。

Note in the tutorial the SignOut button is on Navigator which is inside App . 请注意,在本教程中,“ SignOut按钮位于App内的Navigator上。

BTW you don't necessarily need to wrap SignOut button inside Authenticator . 顺便说一句,您不一定需要在Authenticator包装SignOut按钮。 Put it on anywhere, then show / hide base on Auth.currentAuthenticatedUser() result. 将其放在任何位置,然后根据Auth.currentAuthenticatedUser()结果显示/隐藏。

This will not use the SignOut Component, but is an alternative way of loggin out.You will need to create your own signOut Button. 这不会使用签出组件,而是注销的另一种方法。您将需要创建自己的签出按钮。

This is taken from https://aws-amplify.github.io/docs/js/authentication So in Navbar or where ever you want to create your signOut button, you can add: 这取自https://aws-amplify.github.io/docs/js/authentication。因此,在Navbar或您要创建signOut按钮的任何位置,都可以添加:

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

  // Then in your render method.
  <button onClick={this.signOut} className="signOutButton">SignOut</button>

It requires that you wrapp your App export using "withAuthenticator" 它要求您使用“ withAuthenticator”包装应用导出

so fx In your App.js 所以在您的App.js中

import React, { Component } from "react";
import { withAuthenticator } from "aws-amplify-react";
class App extends Component {
     ...
}
export default withAuthenticator(App, false);

false here means no sigOut button. 如果为false,则表示没有sigOut按钮。 If you try it using true you get the Default signOut button. 如果您使用true进行尝试,则会显示“默认签出”按钮。

After this, you can style the button anyway you like. 之后,您可以随意设置按钮的样式。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM