Skip to content

A simple client-server architecture project built using Flutter for the frontend and Node.js (Express) for the backend. This app demonstrates user authentication with JWT token handling, including user registration, login, login persistence, and logout functionality.

License

Notifications You must be signed in to change notification settings

PAIshanMadusha/json-web-token-authentication

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

17 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ Json Web Token (JWT) Authentication

A simple Client-Server Architecture project built using Flutter for the frontend and Node.js (Express) for the backend. This app demonstrates user authentication with JWT Token handling, including user registration, login, login persistence (token saved in SharedPreferences), secure password hashing using bcryptjs, and logout functionality.


✨ Features:

  • πŸ“ User Registration: (Name, Email, Password, Confirm Password).
  • βœ… Email Validation: Using Regular Expressions.
  • βœ… Password Validation: (minimum 6 characters).
  • πŸ” Password Hashing: Passwords are securely hashed using bcryptjs before storing in MongoDB.
  • πŸ”“ Login with Email and Password: Users can log into the system by entering valid credentials.
  • πŸ”‘ JWT Token Handling: Token generated on successful login, token saved in SharedPreferences for login persistence, and token expiry set to 2 hours.
  • πŸ”„ Logout: Removes token from SharedPreferences.
  • βœ… Navigation: After login, the user navigates to the Main Page.
  • 🎨 Attractive UI Design: The app features a modern and user-friendly interface for a seamless experience.

πŸ“‚ Project Structure:

json-web-token-authentication/
β”œβ”€β”€ flutter_client/
β”‚   β”œβ”€β”€ lib/
β”‚   β”‚   β”œβ”€β”€ pages/
|   |   |   β”œβ”€β”€ authentication_home_page.dart
β”‚   β”‚   β”‚   β”œβ”€β”€ login_page.dart
β”‚   β”‚   β”‚   β”œβ”€β”€ register_page.dart
β”‚   β”‚   β”‚   └── main_page.dart
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   └── authentication_service.dart
β”‚   β”‚   β”œβ”€β”€ widgets/
β”‚   β”‚   |   β”œβ”€β”€ reusable_widgets/
β”‚   β”‚   β”‚   |   β”œβ”€β”€ custom_button.dart
β”‚   β”‚   β”‚   |   └── custom_input_field.dart
β”‚   β”‚   └── main.dart
β”‚   β”œβ”€β”€ pubspec.yaml
β”œβ”€β”€ node_backend/
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── user.js
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── authentication.js
β”‚   β”œβ”€β”€ server.js
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ .env
β”œβ”€β”€ .gitignore
└── README.md

βš™οΈ Tech Stack:

The following tech stack is used in this project:

Parts Technologies Used
πŸ“± Flutter Client Flutter, Dart, HTTP, SharedPreferences, Google Fonts, Flutter SVG
🧱 Node Backend Node.js, Express.js, BcryptJS, JWT, Mongoose, Dotenv, CORS
πŸ›’οΈ Database MongoDB (Localhost)

πŸ“¦ Dependencies:

The following dependencies are used in this project:

πŸ“± Flutter Client:

dependencies:
  http: ^1.3.0
  shared_preferences: ^2.5.3
  google_fonts: ^6.2.1
  flutter_svg: ^2.1.0

🧱 Node Backend:

"dependencies": {
  "axios": "^1.9.0",
  "bcryptjs": "^3.0.2",
  "cors": "^2.8.5",
  "dotenv": "^16.5.0",
  "express": "^5.1.0",
  "jsonwebtoken": "^9.0.2",
  "mongoose": "^8.13.3",
  "nodemon": "^3.1.10"
  }

πŸ“¦ Extra Packages:

  • axios: Installed but not actively used. Needed only if future external API requests are required from the backend.

πŸš€ How to Run the Project:

1. βœ… Frontend Configuration (Flutter):

Clone the repository:

https://github.com/PAIshanMadusha/json-web-token-authentication.git

Navigate to the project directory and to the Client Side:

cd json-web-token-authentication
cd flutter_client

Install dependencies:

flutter pub get

Update the base URL in the client side:

  1. Open flutter_client\lib\services\authentication_service.dart.
  2. Replace the base URL on line 8 (e.g., "http://192.168.8.150:5000/api/auth") with your IP address.

How to find your IP address:

  • Open Command Prompt on your Windows machine.
  • Type ipconfig and press Enter.
  • Look for the IPv4 Address under the Wireless LAN adapter Wi-Fi section and use that as the IP to replace 192.168.8.150.

2. βœ… Backend Configuration (Node.js):

Navigate to the Backend directory:

cd node_backend

Install dependencies:

npm install

3. βœ… Database Setup (MongoDb):

Install MongoDB if you haven’t already, or start it if it’s already installed. Then, create a .env file in the root of the node_backend directory and add the following content:

PORT = 5000
MONGODB_URL = mongodb://127.0.0.1:27017/jwt_authentication
JWT_SECRET = [Your-JWT-Secret]
  • Replace [Your-JWT-Secret] with your actual secret key. You can use any random secure key or generate one using online key generator tools.

Start the backend server:

npm start

If the server starts successfully, you should see the following message in your terminal:

Server Running On Port: 5000
MongoDb Connected Successfully!

βœ… Now you can run the app. Make sure you're inside the flutter_client directory and there are no errors:

cd flutter_client
flutter run

πŸ“Έ System Screenshots:


Screenshot 1Β Β Β  Screenshot 2Β Β Β  Screenshot 3Β Β Β  Screenshot 4Β Β Β 


πŸ‘¨β€πŸ’» Created by:

Ishan Madhusha
GitHub: PAIshanMadusha

Feel free to explore my work and get in touch if you'd like to collaborate! πŸš€


πŸ“ License:

This project is open-source and available under the MIT License.

About

A simple client-server architecture project built using Flutter for the frontend and Node.js (Express) for the backend. This app demonstrates user authentication with JWT token handling, including user registration, login, login persistence, and logout functionality.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published