wiebo-troost / ionic-msal-native

Ionic wrapper for cordova-plugin-msal
MIT License
4 stars 3 forks source link

Not able to get token while login in first attempt. #3

Closed shreya-groot closed 3 years ago

shreya-groot commented 3 years ago
 login(){
      var vm=this;
      console.log("  vm.msal",  vm.msal)
      if(this.platform.is('android')==true){
        const options: any = {
          authorities: [
            {
              type: 'AAD',
              audience: 'AzureADMultipleOrgs',
              authorityUrl: "https://login.microsoftonline.com/organizations",
              default: true
            }
          ], 
          scopes: ['User.Read']
        };

        vm.msal.msalInit(options).then((initResult) => {
            console.log("Success result:", initResult); // "OK"
            vm.value=initResult;
            return initResult;
          },
          (err) => {
            console.log("Error result:", err);
            vm.value=err;
          })
          .then((d) => {
            console.log("d value is",d)
            vm.value="success"
            return vm.msal.signInInteractive()
            .then((v)=>{
          console.log("value after user is signed in",v)
            }).catch((error)=>{
              console.log("error",error)
              vm.value="error"
            });
          })
          .then((jwt) => {
            console.log("Signin result:", jwt);
            vm.value=jwt;
          });
      }

    }

When I tried to login with the code above popup get opened, but after filling the credentials it does not take me back to my Android app at first attempt .After that on closing the login popup once and clicking the above function again will open the popup and after filling the credentails again in it, it ask me to continue and direct me back to the android app with the token generated.

My question is: Why my app didn't get token immediately after filling the credential in first attempt?

wiebo-troost commented 3 years ago

Handing the token back to your app is handled on Android by this activity tag in the AndroidManifest.xml (excerpt from the plugin.xml file of cordova-plugin-msal)

<activity
                android:name="com.microsoft.identity.client.BrowserTabActivity">
                <intent-filter>
                    <action android:name="android.intent.action.VIEW" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.BROWSABLE" />
                    <data
                        android:scheme="msauth"
                        android:host="$PACKAGE_NAME"
                        android:path="/$KEY_HASH" />
                </intent-filter>
            </activity>

You'll need to make sure that the package name and the hash value are correct. Additionally there are some new security rules on Android that may prevent the app from getting the token in some cases. More info here : https://developer.chrome.com/docs/multidevice/android/intents/

shreya-groot commented 3 years ago

This is my AndroidManifest.xml

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="io.ionic.starter" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:networkSecurityConfig="@xml/network_security_config" android:supportsRtl="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.microsoft.identity.client.BrowserTabActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:host="******" android:path="/smM2tjgTjUI********" android:scheme="msauth" />
            </intent-filter>
        </activity>
        <provider android:authorities="${applicationId}.cordova.plugin.camera.provider" android:exported="false" android:grantUriPermissions="true" android:name="org.apache.cordova.camera.FileProvider">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/camera_provider_paths" />
        </provider>
    </application>
    <uses-permission android:name="android.permission.INTERNET " />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE " />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

I found that my package name and key value is correct...I am not able to get the token in first attempt after sign in....Please help me to figure out the problem with my android app.