AWS Amplify自定义登录在安卓端随机卡住pending,iOS及模拟器运行正常
报错根因
你遇到的Pending sign-in attempt already in progress报错是AWS Amplify Auth模块的内置防护逻辑触发导致的:模块内部会为每次登录请求加状态锁,前一次登录流程未完成(可能是请求中、超时未返回、异常终止未释放锁)时,新发起的登录请求都会被拦截抛出该错误,完整报错栈如下:
Pending sign-in attempt already in progress at node_modules/@aws-amplify/auth/lib-esm/Auth.js:535:17 in AuthClass.prototype.signInWithPassword at node_modules/@aws-amplify/auth/lib-esm/Auth.js:421:22 in AuthClass.prototype.signIn at screens/SignInScreen.tsx:47:24 in signInHandler at http://10.129.23.79:19000/node_modules/expo/AppEntry.bundle?platform=android&dev=true&hot=false&minify=false:156070:39 in signInHandler at node_modules/react-native/Libraries/Pressability/Pressability.js:697:17 in _performTransitionSideEffects at node_modules/react-native/Libraries/Pressability/Pressability.js:634:6 in _receiveSignal at node_modules/react-native/Libraries/Pressability/Pressability.js:528:8 in responderEventHandlers.onResponderRelease
安卓端偶现、iOS端正常的核心原因有两个:
- Android系统的触摸事件响应阈值比iOS低,登录按钮容易被误触发多次点击,短时间内发起多个登录请求
- Expo开发模式下安卓真机的热重载逻辑偶尔会残留未释放的登录状态锁,常规重启App也不会自动清理
两台同款Pixel 3a交替出现故障是因为网络差异,当设备网络出现波动、代理/VPN干扰时,Amplify登录请求超时无法正常释放状态锁,下一次发起登录就会触发报错。
修复方案
- 给登录按钮加状态防抖:点击登录后立刻将按钮置为禁用状态、展示加载动画,不管登录请求成功/失败/报错,都在请求结束回调中重置按钮状态,避免重复触发请求,示例逻辑如下:
const [isLoggingIn, setIsLoggingIn] = useState(false) const signInHandler = async (username: string, password: string) => { // 已有登录请求在进行中直接拦截 if (isLoggingIn) return setIsLoggingIn(true) try { // 原有登录逻辑 await Auth.signIn(username, password) // 登录成功后的跳转/状态更新逻辑 } catch (err: any) { // 捕获到pending状态报错时可自动重试一次 if (err?.message === 'Pending sign-in attempt already in progress') { await Auth.signIn(username, password) return } // 其他错误的处理逻辑 } finally { // 无论请求结果如何都重置按钮状态 setIsLoggingIn(false) } }
- 开发环境测试前清理缓存:每次真机测试前先杀掉App进程、清理App存储缓存,再通过Expo Go重新扫码启动,避免残留状态锁影响
- 排查设备网络配置:出现故障时优先检查设备是否开启了VPN、代理、私有DNS,关闭这类配置后再测试登录逻辑
内容的提问来源于stack exchange,提问作者Ramin Zandvakili
相关产品推荐
相关产品推荐

