Create the file mern/server/config.env with your Atlas URI and the server port:
ATLAS_URI=mongodb+srv://<username>:<password>@sandbox.jadwj.mongodb.net/
PORT=5050
ENCRYPTION_KEY=your_64_character_hex_key_here_32byteshexkey1234567890abcdef
GEMINI_API_KEY=your_gemini_api_key
JWT_SECRET=your_super_secret_key
JWT_EXPIRY=1h # token expiration time
GOOGLE_MAPS_API_KEY=
OPENWEATHER_API_KEY=
EMAIL_USER=your-gmail@gmail.com
EMAIL_PASS=your-app-specific-password
Using WSL you can generate encryption key by running the command below in your terminal.
openssl rand -hex 32Using docker, build the containers from the source directory where you find docker-compose.yml file.
docker-compose pullthen run the containers
docker-compose up -dVerify containers are running
docker-compose psTo view the server logs run
docker-compose logs serverchange the script in the server package.json to
"scripts": {
"start": "nodemon --env-file=config.env server",
"test": "echo \"Error: no test specified\" && exit 1"
}If you're not running with docker.
Start server:
cd mern/server
yarn
yarn start
Start Web server
cd mern/client
yarn
yarn dev
This section outlines the changes needed to add authentication to your application and enable saving scores from the ee.html iframe to your database.
-
Install Packages: Install the necessary packages:
passport,passport-local, andmongoose.cd mern/server npm install passport passport-local mongoose -
Create User Model: Create a
user.jsfile in themern/server/modelsdirectory:// mern/server/models/user.js const mongoose = require('mongoose'); const Schema = mongoose.Schema; const userSchema = new Schema({ username: { type: String, required: true, unique: true }, password: { type: String, required: true }, }); module.exports = mongoose.model('User', userSchema);
-
Create Authentication Routes: Create an
auth.jsfile in themern/server/routesdirectory (contents as previously provided). -
Integrate Authentication Routes: In your
mern/server/server.jsfile, add these lines to integrate the authentication routes:const authRoutes = require('./routes/auth'); app.use('/auth', authRoutes);
-
Create Score API Endpoint: Create an API endpoint (e.g.,
/api/scores) in yourserver.jsto handle score submissions. This endpoint should verify the token and save the score to your database. Example:app.post('/api/scores', passport.authenticate('jwt', { session: false }), (req, res) => { // Save score to database using req.body.score and req.user });
-
Create Login Component: Create a
Login.jsxcomponent (contents as previously provided). -
Create Iframe Communicator Component: Create an
IframeCommunicator.jsxcomponent (contents as previously provided). -
Update
App.jsx: Integrate theLoginandIframeCommunicatorcomponents into yourApp.jsx. -
Update
ee.html: Update youree.htmlfile to listen for the authentication token and use it to make authenticated requests to the/api/scoresendpoint. Example:// ... (Inside ee.html) ... <script> // ... (message listener and saveScore function as previously provided) ... </script>
Remember to replace placeholders like API endpoints and database connection strings with your actual values. This README provides a comprehensive guide to implementing the required changes. Thoroughly review and adapt this to your existing codebase.
