This project implements a JSON Web Token (JWT) authentication system using React, JavaScript, and Tailwind CSS. The system allows users to register, login, and access protected routes.
react
: ^18.2.0react-dom
: ^18.2.0react-router-dom
: ^6.3.0jsonwebtoken
: ^9.0.0express
: ^4.18.1express-jwt
: ^7.7.3bcrypt
: ^5.0.1mongodb
: ^4.5.0mongoose
: ^6.3.5tailwindcss
: ^3.1.8
project/
client/
public/
index.html
src/
components/
App.js
Login.js
Register.js
ProtectedRoute.js
containers/
AppContainer.js
LoginContainer.js
RegisterContainer.js
actions/
authActions.js
reducers/
authReducer.js
store.js
index.js
server/
models/
User.js
routes/
auth.js
app.js
package.json
README.md
- Clone the repository:
git clone https://github.com/your-username/project.git
- Install dependencies:
npm install
oryarn install
- Start the server:
npm start
oryarn start
- Start the client:
npm start
oryarn start
in theclient
directory
- Register a new user:
POST /api/register
withusername
,email
, andpassword
in the request body. - Login an existing user:
POST /api/login
withusername
andpassword
in the request body. - Access protected routes:
GET /api/protected
with a valid JWT token in theAuthorization
header.
POST /api/register
: Register a new userPOST /api/login
: Login an existing userGET /api/protected
: Access protected routes
The JWT token is generated using the jsonwebtoken
library and is signed with a secret key. The token is valid for 1 hour and can be refreshed using the refresh
endpoint.
- Passwords are hashed using
bcrypt
before storing in the database. - JWT tokens are verified on each request to ensure authenticity.
Contributions are welcome! Please submit a pull request with a clear description of the changes.
This project is licensed under the MIT License.