AWS通过google登录React Native时出错

时间:2019-07-19 04:40:18

标签: javascript amazon-web-services react-native google-login

我正在通过Google进行AWS登录,我已经登录,并且已经打印了警报。但是问题是我登录后进入catch错误。我已打印警报,如代码中所示,登录后我得到了13条警报。 Alert.alert(“ 13”); 有App.js类,其中Google按钮附加了登录Google。我正在编写auth类的代码,该过程在通过Google进行AWS登录之间进行。我在github中关注了此链接。 https://github.com/patw0929/react-native-amplify-cognito-example

export const loginGoogle = () => {
  return new Promise(async(resolve, reject) => {
    const user = await GoogleSignin.signIn().catch(error => {
      // console.log('WRONG SIGNIN', error);
      console.log('getOpenIdToken', getOpenIdToken);
      Alert.alert("13");
    });
    if (user) {
      const credential = await googleGetCredential();
      if (credential) {
        const result = await getOpenIdToken('accounts.google.com',
          credential.identityId, credential.idToken);
        resolve(result);
        Alert.alert("8");
        return;
      }
      reject('Authentication failed');
      console.log('failed');
      Alert.alert("9");
      return;
    }
    return;
  });
};

export const getOpenIdToken = async(provider, identityId, token) => {
  const payload = {
    IdentityId: identityId,
    Logins: {
      [provider]: token,
    },
  };
  Alert.alert("10");
  try {
    const rsp = await fetch(COGNITO_IDENTITY_API, {
      method: 'POST',
      headers: new Headers({
        'X-Amz-Target': 'AWSCognitoIdentityService.GetOpenIdToken',
        'Content-Type': 'application/x-amz-json-1.1',
        random: new Date().valueOf(),
        'cache-control': 'no-cache',
      }),
      body: JSON.stringify(payload),
    });
    if (!rsp.ok) {
      logout();
    } else {
      const json = await rsp.json();
      return json.Token;
    }
  } catch (e) {
    console.log('Error of getOpenIdToken: ', e);
  }
};

const googleConfigure = () => {
  GoogleSignin.hasPlayServices({
      autoResolve: true
    })
    .then(() =>
      GoogleSignin.configure({
        iosClientId: GOOGLE_SIGNIN_IOS_CLIENT_ID,
        webClientId: GOOGLE_SIGNIN_WEBCLIENT_ID,
      })
    );
};

export const init = () => {
  googleConfigure();
};

我想要令牌以在警报中打印。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

enter image description here布拉姆,在这个问题上,很多信息都丢失了。至少,错误消息必须存在

  1. 首先,您正在使用的库较旧,请尝试使用AWS Amplify,该库的功能要优越得多,并且已经过安全性和错误测试。
  2. 在AWS联合登录中,可以通过两种方式完成,一种使用Cognito用户池,也可以使用Cognito身份池。希望您使用的是Cognito身份池。
  3. 请在“编辑身份池”->“身份验证提供者”->“ Google +
  4. ”中添加Google客户端ID。
  5. 将您要在Google Developer帐户中访问的域列入白名单。

完整文档在这里 https://itnext.io/google-sign-in-using-aws-amplify-and-amazon-cognito-69cc3bf219ad

https://aws.amazon.com/blogs/mobile/amplify-framework-adds-authentication-features-and-enhancements-for-ios-and-android-mobile-sdks/