Apps built with early versions of React Native for Mobile SDK always present a Salesforce login screen at startup. Sometimes, however, these apps can benefit from deferring authentication until some later point. Beginning with React Native for Mobile SDK 4.2, you can defer login to any logical place in your app.
Deferred login implementation is a two-step process:
In your iOS or Android native container app, you call Mobile SDK native methods to disable authentication at startup.
In your React code, you call a Mobile SDK JavaScript function at the point where you plan to initiate authentication.
Mobile SDK provides a React Native template app specifically to demonstrate deferred login: ReactNativeDeferredTemplate. To create an app based on this template, use the forcereact createwithtemplate command, as follows:
1$ forcereact createwithtemplate2Enter the target platform(s) separated by commas (ios, android): ios,android3Enter URI of repo containing template application or a Mobile SDK template name: ReactNativeDeferredTemplate4Enter your application name: MyDeferred5...
Read on for the implementation details.
Step1: Disable Login at Startup
iOS (Objective-C):
To disable the Salesforce login screen from appearing at startup, open your project’s bootconfig.plist file and set shouldAuthenticate to false.
By default, the Salesforce login screen appears at startup. To disable this behavior, override the shouldAuthenticate() method in your MainActivity class (or whichever class subclasses SalesforceReactActivity), as follows:
Step 2: Initiate Authentication in React (JavaScript)
To initiate the authentication process, call the following oauth function:
1function authenticate(success, fail)
This function takes two arguments: a success callback function and a failure callback function. If authentication fails, your failure callback is invoked. If authentication succeeds, your success callback is invoked with a dictionary containing the following keys:
accessToken
refreshToken
clientId
userId
orgId
loginUrl
instanceUrl
userAgent
communityId
communityUrl
We've Moved
Welcome to the new home of the Mobile SDK Developer Guide! For now, the Japanese guide can be found in PDF form.