-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Abhrajitdas02/react-mongo
feat: added signout to `templates/FullStack/React(Frontend)+Nodejs(Backend)`
- Loading branch information
Showing
5 changed files
with
180 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# FullStack Template: React + Node.js | ||
|
||
This repository provides a simple FullStack template for building modern web applications using React for the frontend and Node.js for the backend. It includes essential features such as user authentication, session management, and a responsive user interface. | ||
|
||
## Features | ||
|
||
- **User Authentication**: Supports user registration and login with session management. | ||
- **Frontend**: Built with React, offering a dynamic and responsive user interface. | ||
- **Backend**: Powered by Node.js with Express, providing a robust RESTful API. | ||
- **API Integration**: Seamless communication between the frontend and backend through API calls. | ||
- **Session Management**: Utilizes Express sessions to manage user states. | ||
- **Responsive Design**: Ensures the application is accessible on various devices. | ||
|
||
## Technologies Used | ||
|
||
### Frontend | ||
|
||
- **React**: A JavaScript library for building user interfaces. | ||
- **Bootstrap**: For responsive and mobile-first UI design. | ||
- **React Router**: Handles navigation and routing in the application. | ||
|
||
### Backend | ||
|
||
- **Node.js**: A JavaScript runtime built on Chrome's V8 JavaScript engine. | ||
- **Express**: A minimal and flexible Node.js web application framework. | ||
- **MongoDB**: A NoSQL database known for its flexibility and scalability. | ||
- **Mongoose**: An elegant MongoDB object modeling for Node.js. | ||
|
||
## Installation | ||
1. **Populate the .env.example file**: | ||
|
||
```bash | ||
MONGO_URL | ||
JWT_SECRET | ||
``` | ||
|
||
2. **Install the required dependencies**: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
3. **Ensure MongoDB is running**: | ||
- Make sure your MongoDB server is running locally or accessible remotely. | ||
|
||
4. **Run the application**: | ||
|
||
```bash | ||
npm run dev | ||
both client and server | ||
``` | ||
|
||
5. **Access the application**: | ||
|
||
Visit `http://localhost:5173/` in your web browser. | ||
|
||
## Routes and Functionalities | ||
|
||
- **`/register` [GET, POST]**: | ||
- **GET**: Renders the registration page where new users can sign up. | ||
- **POST**: Handles the form submission for user registration. If the username is already taken, it flashes an error message. Otherwise, it creates a new user and redirects to the login page with a success message. | ||
|
||
- **`/signin` [POST]**: | ||
- **GET**: Renders the login page where users can log in with their credentials. | ||
- **POST**: Handles the login form submission. If the credentials are correct, it logs the user in by storing their user ID in the session and redirects to the dashboard. If the credentials are incorrect, it flashes an error message. | ||
|
||
- **`/signup` [POST]**: | ||
- Displays the dashboard page if the user is logged in. If not, it redirects to the login page. | ||
|
||
- **`/logout` [POST]**: | ||
- Logs the user out by clearing their session and redirects them to the home page. | ||
|
||
- **`/` [GET]**: | ||
- Renders the homepage of the application. | ||
|
||
## Flash Messages | ||
|
||
The application uses flash messages to communicate the following events to the user: | ||
|
||
- **Signup**: | ||
- **Success**: "Signup successful. Please signin." | ||
- **Error**: "Username already exists. Please choose a different one." | ||
|
||
- **Signin**: | ||
- **Error**: "Invalid username or password. Please try again." | ||
|
||
These messages are displayed on the frontend in the registration and login pages. | ||
|
||
## Database | ||
|
||
The application uses MongoDB for storing user information. The `users` collection in the MongoDB database contains the following fields for each user: | ||
|
||
- **_id**: ObjectId, the unique identifier for each document (user). | ||
- **username**: String, unique, cannot be null. | ||
- **emailid**: String, unique, cannot be null. | ||
- **password**: String, hashed, cannot be null. | ||
|
||
--- | ||
|
||
Made using [Universal-Box](https://github.com/Abhishek-Mallick/universal-box) | ||
|
80 changes: 66 additions & 14 deletions
80
templates/FullStack/React(Frontend)+Nodejs(Backend)/client/src/components/Header.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
templates/FullStack/React(Frontend)+Nodejs(Backend)/server/.env.example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
MONGO_URL | ||
JWT_SECRET |
9 changes: 9 additions & 0 deletions
9
templates/FullStack/React(Frontend)+Nodejs(Backend)/server/controllers/user.controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
export const test = (req, res) => { | ||
res.json({ message: 'API is working!'}); | ||
}; | ||
|
||
export const signout = (req, res, next) => { | ||
try { | ||
res.clearCookie('access_token').status(200).json('USer has been signed out!!'); | ||
|
||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
3 changes: 2 additions & 1 deletion
3
templates/FullStack/React(Frontend)+Nodejs(Backend)/server/routes/user.route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import express from "express"; | ||
import { test } from "../controllers/user.controller.js"; | ||
import { signout, test } from "../controllers/user.controller.js"; | ||
|
||
const router = express.Router(); | ||
|
||
router.get("/test", test); | ||
router.post("/signout", signout); | ||
|
||
|
||
export default router; |