capacitor-community / generic-oauth2

Generic Capacitor OAuth 2 client plugin. Stop the war in Ukraine!
MIT License
231 stars 113 forks source link

Unable to access response on android for Azure #174

Closed IronManSastri closed 3 years ago

IronManSastri commented 3 years ago

Description

I am trying to convert a react application in to mobile app using Capacitor and I am using Capaticor-oauth-2 for authentication for azure and google.

Everything was working fine untill i changed my project name. So, i have to restart the whole thing again from the scratch. Followed documentation exactly the same and its working fine in web and IOS.

When i try to work on android, i m not getting the response to the promise resolved function. When i keep console log in the resolve function, they are getting executed but cant console the response.

Here is my code

  const onOAuthBtnClick = () => {
    OAuth2Client.authenticate(authType === "microsoft" ? microsoftOAuthOptions : googleOAuthOptions).then(response => {
//can console only 'accessTokens' name but not anything to response.
      console.log('accessTokens', typeof(response))

      let accessToken = response.authorization_response || response.access_token_response;

      localStorage.setItem('accessToken', accessToken.access_token || accessToken['/access_token']);
      dispatch && dispatch(loginActions.IsUserLoggedIn(true));

    }).catch(reason => {
      console.log('errorr',reason);
      console.error('OAuth rejected', reason);
    });
  }  

Capacitor version:

"@capacitor/android": "^3.2.2",
"@capacitor/cli": "^3.2.2",
"@capacitor/core": "^3.2.2",

Your Plugin Configuration

"@byteowls/capacitor-oauth2": "^3.0.1",
  const microsoftOAuthOptions = {
    appId: 'xxxx',
    authorizationBaseUrl: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
    accessTokenEndpoint: '',
    scope: 'openid',
    responseType: 'token',
    logsEnabled:true,
    web: {
      redirectUrl: 'http://localhost:3000'
    },
    android: {
      pkceEnabled: true,
      responseType: 'code',
      redirectUrl: 'xxxxx://oauth/auth',
      accessTokenEndpoint: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
      // handleResultOnNewIntent: true,
      // handleResultOnActivityResult: true,
    },
    ios: {
      pkceEnabled: true,
      responseType: 'code',
      redirectUrl: 'xxxxxx://oauth/auth',
      accessTokenEndpoint: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
    }
  }

Followed the documentation as it is and changed AndroidManifest.xml, String.xml, build.gradle.

       //AndroidManifest.xml
       <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="@string/custom_url_scheme" android:host="com.evonsyspeoplehcm.app"/>
         </intent-filter>

      //build.gradle
        android.defaultConfig.manifestPlaceholders = [
          // change to the 'custom_url_scheme' value in your strings.xml. They need to be the same. e.g.
          "appAuthRedirectScheme": "com.evonsyspeoplehcm.app"
        ]

      //string.xml69
        <resources>
            <string name="app_name">evonsyspeoplehcm</string>
            <string name="title_activity_main">evonsyspeoplehcm</string>
            <string name="package_name">com.evonsyspeoplehcm.app</string>
            <string name="custom_url_scheme">com.evonsyspeoplehcm.app</string>
        </resources>

Noticed : 2021-09-14 19:31:08.443 14894-14894/com.evonsyspeoplehcm.app D/Capacitor: Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins 112923280 in the log.

Can this be the issue?

Screenshot 2021-09-14 at 5 05 19 PM

@moberwasserlechner , sir your help would be grateful. Thanks

IronManSastri commented 3 years ago

I created a new project and did the whole setup again. working in Ios, but not in android. The auth success function is being called but its not carrying any response. pls help. my setup is pretty much the same as above.

IronManSastri commented 3 years ago

I need to add permission for accessing storage. was able to access response after storing. Thanks