【问题标题】:How to pass an Amplify Cognito session from react native to webview?如何将 Amplify Cognito 会话从 react native 传递到 webview?
【发布时间】:2020-02-20 05:50:58
【问题描述】:

我有一个反应原生应用程序,它呈现 Web 应用程序的 WebView

React Native 应用使用 Cognito 和 Amplify 进行身份验证。 该网络应用程序还使用相同的 Cognito 和 Amplify 进行身份验证。

我在 react native 中构建了一个登录流程,该流程具有电子邮件/密码登录和社交媒体联合 Oauth 登录。这两个登录流程都在 react native 空间中成功运行并返回一个

CognitoUserSession {
  idToken: CognitoIdToken, 
  refreshToken: CognitoRefreshToken, 
  accessToken: CognitoAccessToken, 
  clockDrift: 0
}

当 React Native 应用程序呈现 WebView 时,Web 应用程序未经身份验证。我能够成功地将CognitoUserSession 数据传递到 WebView 中。不幸的是,我看不到让Amplify 对该会话重新进行身份验证的方法。

【问题讨论】:

    标签: react-native webview amazon-cognito aws-amplify


    【解决方案1】:

    这是我编写的 mobileLogin 函数

    import Amplify, { Auth } from 'aws-amplify';
    import {
      CognitoUser,
      CognitoUserSession,
      CognitoIdToken,
      CognitoRefreshToken,
      CognitoAccessToken,
    } from 'amazon-cognito-identity-js';
    
    window.mobileLogin = async function(mobileSession) {
      amplify = Amplify.configure({
        ...config().amplify,
        userPoolWebClientId: '', //switch to mobile client
      });
    
    const localSession = new CognitoUserSession({
      IdToken: new CognitoIdToken({ IdToken: mobileSession.idToken.jwtToken }),
      RefreshToken: new CognitoRefreshToken({ RefreshToken: mobileSession.refreshToken }),
      AccessToken: new CognitoAccessToken({ AccessToken: mobileSession.accessToken.jwtToken }),
    });
    
    const localUser = new CognitoUser({
      Username: mobileSession.accessToken.payload.username,
      Pool: Auth.userPool,
      Storage: Auth.userPool.storage,
    });
    localUser.setSignInUserSession(localSession);
    
    // this seems like a hack
    Auth.currentCredentials = async () => localSession;
    
    try {
      await Auth.currentSession();
      console.warn(`mobile login current session!!`);
      store.dispatch(silentReloginAction())
    } catch (ex) {
      console.warn(`mobile login ${ex}`);
    }
     };
    }
    

    【讨论】:

    • 我有类似的问题,但我只有在网络应用程序中的身份验证流程,并且本机 android 应用程序将其部署在网络活动中。但是,我需要会话数据来持久化,这样 android 用户就不需要每次都登录,但是 Auth.currentSession() (在网络应用程序中)没有找到任何东西。网络活动不存储此数据。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 2020-08-21
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 2020-03-26
    • 2023-03-29
    相关资源
    最近更新 更多