capacitor-community / generic-oauth2

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

Auth code is returnedby provider but error from plugin: Authorization failed on IOS - ERR_NO_AUTHORIZATION_CODE #279

Open ap0894 opened 3 weeks ago

ap0894 commented 3 weeks ago

Description

On platform iOS after user sucessfully completes login on OAUTH provider page user is redirected back to the app where the following is logged in XCode indicating that code parameter is correctly set in callback but plugin is erroring with ERR_NO_AUTHORIZATION_CODE

{"iosOpenInPlace":"","url":"com.refsix:\/\/oauth?code=kM1GbawNyK5xAU0m7SnpbIBy0RVWMatcR11_oxd3Ms4&state=LrRyMwj0TnbnjlobfIPI","iosSourceApplication":"com.apple.SafariViewService"}
I/Capacitor/GenericOAuth2Plugin: Authorization failed with The operation couldn’t be completed. (OAuthSwiftError error -11.)
ERROR MESSAGE:  {"message":"ERR_NO_AUTHORIZATION_CODE","errorMessage":"ERR_NO_AUTHORIZATION_CODE"}

Capacitor version:

Run npx cap doctor:

Latest Dependencies:

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

Installed Dependencies:

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

[info] Using Gemfile: RubyGems bundle installed
[success] iOS looking great! 👌
[success] Android looking great! 👌

Library version:

@capacitor-community/generic-oauth2@6.1.0

OAuth Provider:

Assignr. Documentation: https://assignr-api.readme.io/reference/authentication

Your Plugin Configuration

const startOAuthFlow = async () => {
        setLoading(true);
        try {
            console.log('Starting OAuth flow...');
            const result = await GenericOAuth2.authenticate({
                authorizationBaseUrl: 'https://api.assignr.com/oauth/authorize',
                accessTokenEndpoint: 'https://api.assignr.com/oauth/token',
                appId: CLIENT_ID,
                scope: 'games.read account.read',
                resourceUrl: '',
                logsEnabled: true,
                web: {
                    appId: CLIENT_ID,
                    responseType: "code",
                    accessTokenEndpoint: "https://api.assignr.com/oauth/token",
                    redirectUrl: REDIRECT_URI_WEB,
                    windowOptions: "height=600,left=0,top=0"
                },
                android: {
                    appId: CLIENT_ID,
                    responseType: "code",
                    pkceEnabled: true,
                    redirectUrl: REDIRECT_URI_ANDROID
                },
                ios: {
                    appId: CLIENT_ID,
                    responseType: "code",
                    pkceEnabled: true,
                    redirectUrl: REDIRECT_URI_IOS
                }
            });

            console.log('Authorization Code: ', result.authorizationCode);
            window.dispatchEvent(new CustomEvent('AuthorizationCodeReceived', { detail: result.authorizationCode }));
            setLoading(false);
        } catch (error: any) {
            console.error('OAuth2 Error:', JSON.stringify(error, null, 2));
            if (error.message === 'USER_CANCELLED') {
                console.log('User cancelled the authentication process.');
            } else {
                console.error('Error Details:', error);
            }
            setLoading(false);
        }
    };

Affected Platform(s):

niteshshyam commented 2 weeks ago

Same exact issue here with Spotify OAuth. Similar behaviour to the above. It goes through the Spotify login fine, asking for user permissions and redirects to my iOS app but I get the error: I/Capacitor/GenericOAuth2Plugin: Authorization failed with The operation couldn’t be completed. (OAuthSwiftError error -1.) ERROR MESSAGE: {"message":"ERR_NO_AUTHORIZATION_CODE","errorMessage":"ERR_NO_AUTHORIZATION_CODE"} ⚡️ [error] - {"message":"ERR_NO_AUTHORIZATION_CODE","errorMessage":"ERR_NO_AUTHORIZATION_CODE"} ⚡️ [error] - {"errorMessage":"ERR_NO_AUTHORIZATION_CODE"}