gpt4 book ai didi

react-native - 如何在使用 CUSTOM AUTH 身份验证流程登录期间保留 Cognito 用户

转载 作者:行者123 更新时间:2023-12-04 11:36:54 26 4
gpt4 key购买 nike

我的 React Native 应用程序将 Amplify 用于 CUSTOM_AUTH身份验证流程。用户通过电子邮件收到一个链接,以满足challengeAnswer 请求。过程是这样的:

用户发起登录:

const cognitoUser = await Auth.signIn(username);

电子邮件通过 lambda 发送给用户。

用户离开应用程序以检索电子邮件。

用户单击电子邮件中的链接,通过 RN Linking 将用户路由回应用程序接口(interface)。

链接中的代码使用以下方式处理:
await Auth.sendCustomChallengeAnswer(
cognitoUser,
authChallengeAnswer
);

通常这很有效,但不能保证 cognitoUser当用户检索电子邮件时,应用程序在后台运行后,对象将存在。在此期间,iOS 转储应用程序的可能性非零, cognitoUser var 将消失,迫使用户重新启动登录过程。我正在寻找一种方法来坚持 cognitoUser以某种方式对象,所以如果 iOS 决定应用程序需要死这个 var 可以从缓存中检索。

我可以将对象缓存到 Amplify 缓存 (AsyncStorage) 中
await Cache.setItem("cognitoUser", cognitoUser);

然后用
await Cache.getItem("cognitoUser");

失败了

TypeError: user.sendCustomChallengeAnswer is not a function



因为缓存它的过程丢失了所有的 __proto__职能。它只是作为基本对象检索。

我怀疑原因是我没有使用 TypeScript,并且对象以某种方式丢失了一些类型信息。

有没有更好的方法来持久化这个 CognitoUser 对象,以便我可以保证在用户离开/返回应用程序后它存在于 CUSTOM_AUTH 中。流动。

最佳答案

我使用以下代码在使用 CUSTOM_AUTH 身份验证流程登录期间保留 CognitoUser:

import Auth from '@aws-amplify/auth'
import { CognitoUser } from 'amazon-cognito-identity-js'

const CUSTOM_AUTH_TTL = 5 * 60 * 1000 // Milliseconds

interface CustomAuthSession {
username: string
session: string
// Milliseconds after epoch
expiresAt: number
}

function clearCustomAuthSession() {
window.localStorage.removeItem('CustomAuthSession')
}

function loadCustomAuthSession(): CognitoUser {
const raw = window.localStorage.getItem('CustomAuthSession')
if (!raw) {
throw new Error('No custom auth session')
}
const storedSession: CustomAuthSession = window.JSON.parse(raw)
if (storedSession.expiresAt < window.Date.now()) {
clearCustomAuthSession()
throw new Error('Stored custom auth session has expired')
}
const username = storedSession.username
// Accessing private method of Auth here which is BAD, but it's still the
// safest way to restore the custom auth session from local storage, as there
// is no interface that lets us do it.
// (If we created a new user pool object here instead to pass to a
// CognitoUser constructor that would likely result in hard to catch bugs,
// as Auth can assume that all CognitoUsers passed to it come from its pool
// object.)
const user: CognitoUser = (Auth as any).createCognitoUser(username)
// Session is not exposed to TypeScript, but it's a public member in the
// JS code.
;(user as any).Session = storedSession.session
return user
}

function storeCustomAuthSession(cognitoUser: CognitoUser) {
// Session isn't exposed to TypeScript, but it's a public member in JS
const session = (cognitoUser as any).Session
const expiresAt = window.Date.now() + CUSTOM_AUTH_TTL
const otpSession: CustomAuthSession = {
session,
expiresAt,
username: cognitoUser.getUsername(),
}
const json = window.JSON.stringify(otpSession)
window.localStorage.setItem('CustomAuthSession', json)
}

关于react-native - 如何在使用 CUSTOM AUTH 身份验证流程登录期间保留 Cognito 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54847779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com