FormidableLabs / react-native-app-auth

React native bridge for AppAuth - an SDK for communicating with OAuth2 providers
https://commerce.nearform.com/open-source/react-native-app-auth
MIT License
1.96k stars 437 forks source link

salesforce? #759

Open billnbell opened 1 year ago

billnbell commented 1 year ago

How do you setup salesforce with this?

paritosh-yadav commented 1 year ago

Any update on this?

billnbell commented 1 year ago

I was able to get it to work. We really should document it.

     const callbackUrl =
        appEnv === 'PROD'
          ? 'https://login.salesforce.com/services/oauth2/authorize'
          : 'https://sandboxname.sandbox.my.salesforce.com/services/oauth2/authorize';

      const scopes = ['api', 'web', 'refresh_token']; // must match in connected apps in SF, redirect and secrets must match too

      let config: AuthConfiguration = {
        issuer: callbackUrl, // see above
        clientId: consumerKey, // get from SF
        clientSecret: consumerSecret, // get from SF
        redirectUrl, // must be registered in app, and setup in SF
        scopes, // must match to SF
        serviceConfiguration: {
          authorizationEndpoint: callbackUrl,
          tokenEndpoint:
            appEnv === 'PROD'
              ? 'https://login.salesforce.com/services/oauth2/token'
              : 'https://sandboxname.sandbox.my.salesforce.com/services/oauth2/token',
          revocationEndpoint:
            appEnv === 'PROD'
              ? 'https://login.salesforce.com/services/oauth2/revoke'
              : 'https://sandboxname.sandbox.my.salesforce.com/services/oauth2/revoke',
        },
      };

Ok then when setup call:

        const result = await authorize(config);
        console.log(result);
        if (result) {
          const { refreshToken, accessToken, tokenAdditionalParameters } = result;
          console.log(`refreshToken: ${refreshToken} accessToken: ${accessToken}`);
        }
billnbell commented 1 year ago

Also if you want for force login - like after clicking logout button - add to the config.

config = { ...config, additionalParameters: { prompt: 'login' } };
billnbell commented 1 year ago

OK no idea but I am getting on Android: JSON deserialization error

billnbell commented 1 year ago

OK for Android you cannot have issuer and serviceConfiguration.

It works fine with issuer. Just give it the main URL like "https://login.salesforce.com" or "https://sandboxname.sandbox.my.salesforce.com"

  let config: AuthConfiguration = {
    issuer: salesforceIssuerUrl,
    clientId: consumerKey,
    clientSecret: consumerSecret,
    redirectUrl,
    scopes,
  };