capacitor-community / generic-oauth2

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

HELP: Getting "USER_CANCELLED" error with Google oAuth2 in Android Simulator #227

Open jordanranson opened 1 year ago

jordanranson commented 1 year ago

Description

Hello, thanks for taking the time to look at my request. I'm not a mobile developer and have been banging my head against the wall for a few days now trying to figure out why I would get a USER_CANCELLED error when the oAuth flow appears to be working in the app. I'd appreciate any hints or advice on what I can do to get this working. I even followed the additional instructions for USER_CANCELLED issues that are mentioned here (https://github.com/moberwasserlechner/capacitor-oauth2#android-1), but no luck (I am not using Azure).

Thanks!


Issue:

Getting "USER_CANCELLED" error with Google oAuth2 in Android Simulator but everything seems to be setup correctly and the oAuth flow successfully completes in the simulator.

Example flow:

  1. Click Google login button, which triggers OAuth2Client.authenticate
  2. Google oAuth login dialog appears. Sign in successfully, dialog closes.
  3. Return to app, USER_CANCELLED error coming from rejected promise in JavaScript/LogCat console. No additional details. I do not have access to oAuth logs on Google's side at the moment.

LogCat output:

D/Capacitor: App started
D/Capacitor: Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins 1765555632
D/Capacitor: Sending plugin error: {"save":false,"callbackId":"25800850","pluginId":"OAuth2Client","methodName":"authenticate","success":false,"error":{"message":"USER_CANCELLED"}}
D/Capacitor: App resumed

JS Console output:

result OAuth2Client.authenticate (#104032769)
{message: "USER_CANCELLED"}

Capacitor version:

Run npx cap doctor:

Latest Dependencies:

@capacitor/cli: 4.6.2 @capacitor/core: 4.6.2 @capacitor/android: 4.6.2 @capacitor/ios: 4.6.2

Installed Dependencies:

@capacitor/core: 3.4.3 @capacitor/ios: 3.4.3 @capacitor/cli: 3.4.3 @capacitor/android: 3.4.3


### Library version:
<!-- Please remove all items that are not relevant. -->

- 3.0.1

### OAuth Provider:
<!-- Please remove all items that are not relevant. -->

- Google

### Your Plugin Configuration
<!-- Without secret stuff (of course). -->

```typescript
// capacitor.config.json
{
  "appId": "com.addictinggames.starveio",
  "appName": "client",
  "webDir": "www",
  "bundledWebRuntime": false,
  "server": {
    "hostname": "capacitor.starve.io",
    "androidScheme": "https"
  },
  "plugins": {
    "CapacitorCookies": {
      "enabled": true
    }
  }
}

// OAuth2Client.authenticate options
{
        authorizationBaseUrl: "https://accounts.google.com/o/oauth2/auth",
        accessTokenEndpoint: "https://www.googleapis.com/oauth2/v4/token",
        scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile",
        resourceUrl: "https://www.googleapis.com/userinfo/v2/me",
        logsEnabled: true,
        web: {
            appId: "xxxxxx.apps.googleusercontent.com",
            responseType: "token", // implicit flow
            accessTokenEndpoint: "", // clear the tokenEndpoint as we know that implicit flow gets the accessToken from the authorizationRequest
            redirectUrl: "http://localhost:8080",
            windowOptions: "height=600,left=0,top=0"
        },
        android: {
            appId: "xxxxxx.apps.googleusercontent.com",
            responseType: "code", // if you configured a android app in google dev console the value must be "code"
            redirectUrl: "com.addictinggames.starveio:/" // package name from google dev console
        },
        ios: {
            appId: "xxxxxx.apps.googleusercontent.com",
            responseType: "code", // if you configured a ios app in google dev console the value must be "code"
            redirectUrl: "com.starveio.dev:/" // Bundle ID from google dev console
        }
    }

Affected Platform(s):

raphael-yapla commented 9 months ago

I faced exactly the same issue with an AppLink domain and ended up making it work by adding the net.openid.appauth.RedirectUriReceiverActivity like you did but I had to remove the original intent-filter from the MainActivity. Hope that helps!