【问题标题】:Auth.signOut dosent sign out as expectedAuth.signOut 按预期退出
【发布时间】:2020-09-01 10:53:38
【问题描述】:

我正在使用 AWS Amplify 和 React 构建我的第一个应用程序,并且我正在使用 withAuthenticator 强制用户登录。当我点击退出按钮时,它会重定向回登录页面,但如果我刷新页面,它会重新登录进入并再次显示我的应用程序,显然,这没有用,因为我需要它来将用户完全退出我的应用程序并从浏览器中删除他们的所有数据。我究竟做错了什么?我什至制作了一个按钮来触发 Auth.signOut() 但我仍然遇到同样的问题,即我无法从我的应用程序中注销。

我什至在这里使用了全局登录,但是当我刷新页面时它仍在重新登录。 https://docs.amplify.aws/lib/auth/emailpassword/q/platform/js#sign-out

我的代码:

import React from 'react';
import logo from './SE_logo.jpg';
import './App.css';
import Amplify, { API, Auth } from 'aws-amplify';
import {withAuthenticator} from 'aws-amplify-react';
import '@aws-amplify/ui/dist/style.css';

async function onSignOutClick() {
  //await Auth.signOut()
  //    .then(data => console.log(data))
  //    .catch(err => console.log(err));
  console.log(Auth.signOut());
 }

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <button onClick={onSignOutClick}>Log Out</button>
      </header>
    </div>
  );
}

export default withAuthenticator(App, true);

【问题讨论】:

  • 您是否使用某种 localStorage 或 cookie 来保存用户的 authenticated 状态?
  • 我不确定我才刚刚开始使用 Amplify 和身份验证,所以我将所有内容都留给了 Amplify 框架,所以大概它会将访问令牌存储在某处,因此当我刷新它找到的应用程序时他们知道他们仍然有效并直接进入应用程序而不是再次提示登录。

标签: javascript reactjs aws-amplify


【解决方案1】:

您可以像这样在注销后尝试重定向用户

async function handleLogout() {
  await Auth.signOut();

  userHasAuthenticated(false);

  history.push("/login");
}

您可以使用本教程设置浏览器历史记录并重定向用户: https://serverless-stack.com/chapters/redirect-on-login-and-logout.html

【讨论】:

  • 我得到的只是 userHasAuthenticated 没有定义
  • 您必须创建一个新状态 userHasAuthenticated 来跟踪用户状态,但您也可以暂时删除该行,因为主要目标是将用户重定向到登录屏幕
  • 当我调用 Auth.signOut 时重定向到登录屏幕从来都不是问题,它会重定向回登录屏幕,但随后我单击刷新或按 F5 它会返回到我的应用程序,所以基本上重新登录自动,我需要它完全注销,就像一个新人打开浏览器并访问我的网站一样,它会以以前的用户身份登录。
【解决方案2】:

删除您的本地存储数据/cookies/等 要么 试试这个

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

【讨论】:

  • 不,它会重定向回登录页面,但如果我按 F5,它会直接登录回应用程序。
  • 能否提供auth.Signout请求的console.log?
  • 它返回一个状态为已解决且值为未定义的承诺
  • 能否提供样品?
  • 我已添加到我的问题中 :)
【解决方案3】:

我通过从我的 Apmlify.configure 部分中删除以下代码来修复它,不确定它的作用,但我在 AWS 文档中,所以我把它留在了我的。我试图删除所有 cookie 和本地存储内容,但它破坏了我的登录,只是一直显示没有当前用户,所以我发现删除下面的代码是解决这个问题的方法,它也解决了我未注销的问题?

      cookieStorage: {
      // REQUIRED - Cookie domain (only required if cookieStorage is provided)
          domain: '.mydomain.co.uk',
      // OPTIONAL - Cookie path
          path: '/',
      // OPTIONAL - Cookie expiration in days
          expires: 365,
      // OPTIONAL - Cookie secure flag
      // Either true or false, indicating if the cookie transmission requires a secure protocol (https).
          secure: true
      },

【讨论】:

    【解决方案4】:

    在角度你可以做到这一点....

    signOut(){
        console.log('...signing out ....');
        Auth.signOut();
        this.router.navigate(['/login']);
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 2019-12-09
      相关资源
      最近更新 更多