Run the following in the root directory of your NPM project.
See Customized Dependencies (Advanced) below for customizing dependencies.
First, you need to create the server-side Stitch app, and (for the purpose of this quick start) enable anonymous authentication:
For detailed instructions, see Create a Stitch App.
Next, you create the source for your client app.
npminstalled. See npmjs.com.
npm install mongodb-stitch-react-native-sdk.
npm installagain to ensure that the SDK's dependencies are properly fetched.
App.js, replace the existing code with the following, replacing
<your-client-app-id>with the id you retrieved when setting up the application in MongoDB Stitch:
npm startand following the instructions in the terminal.
When your app has started, use Stitch.initializeDefaultAppClient to initialize the Stitch SDK. Replace
<your-client-app-id> with your Stitch application's client app ID:
Note: Unlike the other Stitch SDKs, this client initialization method is asynchronous. This is due to the fact that the client stores persisted authentication information using React Native's AsyncStorage system.
We enabled anonymous authentication in the steps above, so let's log in with it! Add the following anywhere in your code:
When running this code, you should see the following in your standard out:
See StitchAuth for more information.
Assuming you've configured your Stitch application to have a function named "echoArg" that returns its argument, you should see a message like:
echoArg Function in Stitch would look something like:
As a convenience, the SDK includes the bson library. You can import it as you would import other classes and values from the SDK.
Here is an example of importing BSON to generate a BSON
For customized dependencies, use the following:
In the case that you don't want a single default initialized
StitchAppClient, you can use the following with as many client app IDs as you'd like to initialize clients for multiple app IDs:
You can use the client returned there or anywhere else in your app by using the following:
The client maintains some background processes in the event loop that must be
shutdown when the client is no longer needed. Simply call
close on the client
when you are done with the client:
A discriminated union type representing an auth event that might occur and should be sent to any underlying listeners.
Generated using TypeDoc