You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Amplify V6后React Native社交登录异常问题求助

解决方案

1. 修正V6的Auth配置结构

V6中,OAuth相关配置不再是顶层字段,而是嵌套在auth.oauth下,调整你的配置代码如下:

import { Amplify } from 'aws-amplify';
import awsExports from './aws-exports';
import { Platform, Linking, WebBrowser } from 'react-native';

async function urlOpener(url: string, redirectUrl: string): Promise<void> {
    console.log('custom urlOpener');
    const authSessionResult = await WebBrowser.openAuthSessionAsync(url, redirectUrl);

    console.log(authSessionResult);

    if (authSessionResult.type === 'success' && Platform.OS === 'ios') {
        WebBrowser.dismissBrowser();
        await Linking.openURL(authSessionResult.url);
    }
}

Amplify.configure({
    ...awsExports,
    auth: {
        ...awsExports.auth,
        oauth: {
            ...awsExports.oauth,
            urlOpener,
            redirectSignIn: awsExports.oauth.redirectSignIn,
            redirectSignOut: awsExports.oauth.redirectSignOut,
        },
    },
});

2. 处理OAuth重定向回调,完成认证流程

V6中,OAuth登录跳转回App后,必须主动调用handleSignInRedirect完成认证信息同步,否则getCurrentUser无法获取用户数据。在App入口组件中添加监听逻辑:

import { useEffect } from 'react';
import { Linking } from 'react-native';
import { handleSignInRedirect, getCurrentUser } from 'aws-amplify/auth';
import awsExports from './aws-exports';

function App() {
    useEffect(() => {
        const processRedirectUrl = async (url: string) => {
            if (url.startsWith(awsExports.oauth.redirectSignIn)) {
                try {
                    // 处理重定向,同步用户认证状态
                    await handleSignInRedirect();
                    // 此时可正常获取用户信息
                    const currentUser = await getCurrentUser();
                    console.log('当前用户信息:', currentUser);
                } catch (err) {
                    console.error('重定向处理失败:', err);
                }
            }
        };

        // 初始化时检查是否有未处理的重定向URL
        Linking.getInitialURL().then(processRedirectUrl);
        // 监听后续URL跳转事件
        const urlSubscription = Linking.addEventListener('url', (event) => processRedirectUrl(event.url));

        return () => urlSubscription.remove();
    }, []);

    // ... 其余组件逻辑
}

export default App;

3. 验证iOS URL Scheme配置

确保iOS项目的Info.plist中已正确配置重定向URL对应的Scheme,例如你的redirectSignIn为myapp://auth,则添加:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myapp</string>
        </array>
    </dict>
</array>

说明

调整配置后,自定义urlOpener会被正常调用,重定向回调处理完成后,getCurrentUser即可获取到用户信息,同时不会再出现“用户已登录但无法获取信息”的矛盾状态。

内容的提问来源于stack exchange,提问作者Andrija Gajic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 02:23:32