MERN Stack authentication app
-
RUN npm install from the root dir (assume you have node.js installed).
-
Create .env file in the root dir & add the environment variables as follows (I have mongodb installed locally, you can replace the values with yours)
Example .env file:
PORT=8000
NODE_ENV=development
JWT_SECRET_KEY=Zmp3b3JpNDM5NDgzOT4930reirjerkqwqw
MONGODB_CONNECTION_URL=mongodb://localhost:27017/db-auth -
RUN the backend API (You can run the API using either one npm scripts listed below) From the root dir,
- npm start
- npm run server (It will use nodemon & restart the server automatically in every signle change so recommended to use in development)
Now you have the API server up and running, How do you test it?
Here are some endpoints that you can test and make request to, using any API clients like Postman
- POST /api/users/auth
You make a POST request to this endpoint in order to authenticate(login) a user with request body (email, password) - POST /api/users
You make a POST request to this endpoint in order to create(register) a user with request body (name, email, password) - POST /api/users/logout
You make a POST request to this endpoint in order to logout a user - GET /api/users/profile
It is a protected route. You make a GET request to this endpoint in order to retrive the current user - PUT /api/users/profile
It is a protected route. You make a PUT request to this endpoint in order to update the current user data with request body (name, email, password)
Make sure you prefix all the route with your base URL.
For example: (with value of the PORT defined in the .env above): http://localhost:8000
For more detailed Info You can always feel free to check the code :)