最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

React Native Expo AuthSession using Azure Authentication Not Working on Android - Stack Overflow

programmeradmin2浏览0评论

I am trying to implement Azure Authentication using Expo AuthSession on React Native Expo as per Documentation /guides/authentication/#azure.

This Azure Authentication process worked fine on IOS and thus generated access token closing browser automatically, Whereas on Android it's not Working. Neither it was generating access token and nor closing browser.

Here are the Working Screenshots of IOS App.

IOS Page Before Authentication IOS Azure Authentication Process IOS Azure Authentication Success with Token

Here are not Working Screenshots of Android App. Android Page Before Authentication Android Azure Authentication Process

Code Snippet for Azure Expo AuthSession

import { useState } from 'react';
import * as WebBrowser from 'expo-web-browser';
import {
  exchangeCodeAsync,
  makeRedirectUri,
  useAuthRequest,
  useAutoDiscovery,
} from 'expo-auth-session';
import { Button, Text, SafeAreaView, Platform } from 'react-native';


WebBrowser.maybeCompleteAuthSession();


export default function App() {
  // Endpoint
  const discovery = useAutoDiscovery(
    '/<TENANT_ID>/v2.0',
  );

  const redirectUri = makeRedirectUri({
        native: Platform.OS === 'ios' ? "msauth.myorg.myauthapp://auth" : "msauth://com.myorg.myauthapp/GTEWOFHNVBCDAPDICYVBSQTY%2B",
    });

  const clientId = '<CLIENT_ID>';

  // We store the JWT in here
  const [token, setToken] = useState<string | null>(null);

  // Request
  const [request, , promptAsync] = useAuthRequest(
    {
      clientId,
      scopes: ['openid', 'profile', 'email', 'offline_access'],
      redirectUri,
    },
    discovery,
  );

  return (
    <SafeAreaView>
      <Button
        disabled={!request}
        title="Login"
        onPress={() => {
          promptAsync().then((codeResponse) => {
            if (request && codeResponse?.type === 'success' && discovery) {
              exchangeCodeAsync(
                {
                  clientId,
                  code: codeResponse.params.code,
                  extraParams: request.codeVerifier
                    ? { code_verifier: request.codeVerifier }
                    : undefined,
                  redirectUri,
                },
                discovery,
              ).then((res) => {
                setToken(res.accessToken);
              });
            }
          });
        }}
      />
      <Text>{token}</Text>
    </SafeAreaView>
  );
}

I tried to implement this using expo-web-browser's WebBrowser.openAuthSessionAsync() function, but getting same issue in the end.

Also i tried to change android/app/src/AndroidManifest.xml file as per this post expo authsession not closing when receiving redirecturi on android and still no luck.

I have checked this on devices with expo development builds for android.

 <application android:name=".MainApplication" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:allowBackup="true" android:theme="@style/AppTheme" android:supportsRtl="true">
    <meta-data android:name="expo.modules.updates.ENABLED" android:value="false"/>
    <meta-data android:name="expo.modules.updates.EXPO_UPDATES_CHECK_ON_LAUNCH" android:value="ALWAYS"/>
    <meta-data android:name="expo.modules.updates.EXPO_UPDATES_LAUNCH_WAIT_MS" android:value="0"/>
    <activity android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize" android:theme="@style/Theme.App.SplashScreen" android:exported="true" android:screenOrientation="portrait">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
      <intent-filter android:label="auth">
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
         <data android:scheme="myappscheme"   android:path="msauth://com.myorg.myauthapp/GTEWOFHNVBCDAPDICYVBSQTY%2B" 
         />
      </intent-filter>
    </activity>
  </application>
发布评论

评论列表(0)

  1. 暂无评论