I'm struggling to grasp the concept of connecting my React Native app to the MongoDB Atlas Cluster. Essentially, I want to retrieve user data (username and password) from a login component page and verify its existence within the Atlas Cluster database.
Currently, my app is built using React Native with Expo. The login page functions as expected, allowing users to input their credentials.
The objective is to utilize the provided code snippet (obtained from the Atlas documentation on Connection Strings) to establish a connection and perform the necessary checks:
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<userName>:<password>@testcluster1-dbdq3.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// actions performed on the collection object
client.close();
});
Considering that React Native sets up a server, I am uncertain if Express needs to be implemented. As a novice in this area, I am exploring suitable packages such as mongoose or MongoDB (from NPM). Understanding the fundamental logic behind these components and required packages remains crucial.
The primary goal is to validate the user ID and password against the database when logging in. If the entry does not exist, an option for registration will prompt the user to provide additional information - subsequently, adding a new user to the database.
Essentially, I aim to comprehend the following aspects regarding the code structure:
- The process of establishing a connection to the database within the application and determining when this connection should occur (upon app launch or upon clicking the login button).
- Retrieval of username and password data to conduct a search within the Atlas database for user verification. Upon successful validation, the subsequent page should load.
- In cases where the provided username and password do not match any existing records, the procedure involves writing the new user's credentials to the database.
Your guidance on these matters would be greatly appreciated.