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.
- π 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.
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
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) |
The following dependencies are used in this project:
dependencies:
http: ^1.3.0
shared_preferences: ^2.5.3
google_fonts: ^6.2.1
flutter_svg: ^2.1.0
"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"
}
- axios: Installed but not actively used. Needed only if future external API requests are required from the backend.
https://github.com/PAIshanMadusha/json-web-token-authentication.git
cd json-web-token-authentication
cd flutter_client
flutter pub get
- Open
flutter_client\lib\services\authentication_service.dart
. - 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
.
cd node_backend
npm install
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.
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
Ishan Madhusha
GitHub: PAIshanMadusha
Feel free to explore my work and get in touch if you'd like to collaborate! π
This project is open-source and available under the MIT License.