A professional-grade authentication system implementing secure user registration, login, and session management with JWT.
- Secure user registration with password hashing
- JWT-based authentication
- Session management
- Role-based access control
- Password reset functionality
- Protection against common security vulnerabilities
- Modern interactive UI: Login, Signup, and Dashboard pages
- Python (Flask)
- PostgreSQL
- JWT Authentication
- Bcrypt encryption
- HTML, CSS, JavaScript (for frontend UI)
This document provides step-by-step instructions to set up and run the Secure Authentication System.
- Python 3.8 or higher
- PostgreSQL (portable or installed version)
- pip (Python package manager)
SAS/
├── app/ # Application package
│ ├── config/ # Configuration files
│ ├── models/ # Database models
│ ├── routes/ # API routes
│ ├── templates/ # HTML templates
│ ├── static/ # Static files
│ ├── utils/ # Utility functions
│ └── database.py # Database connection
├── app.py # Main application entry point
├── init_db.py # Database initialization script
├── create_admin.py # Admin user creation script
├── setup.py # Setup script
├── requirements.txt # Python dependencies
├── start_postgres.bat # PostgreSQL startup script (Windows)
├── test_api.py # API testing script
└── README.md # Project README
If you're using PostgreSQL portable:
- Edit
start_postgres.bat
and update thePGBIN
path to point to your PostgreSQL bin directory - Run
start_postgres.bat
to start the PostgreSQL server - Note: You may need to create a user and password that match the ones in your
.env
file
If you're using installed PostgreSQL:
- Make sure PostgreSQL is running
- Create a new database called
sas_db
or use an existing one and update the connection string in.env
Create a .env
file in the project root with the following content:
SECRET_KEY=your_secret_key_here
JWT_SECRET_KEY=your_jwt_secret_key_here
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/sas_db
FLASK_ENV=development
Alternatively, run python setup.py
which will create this file for you with random secret keys.
pip install -r requirements.txt
python init_db.py
python create_admin.py <username> <email> <password>
Example:
python create_admin.py admin admin@example.com Admin123!
Alternatively, you can run python setup.py
which will guide you through the admin user creation process.
python app.py
The application will start and be available at http://localhost:5000
To test the API endpoints, run:
python test_api.py
This will execute a series of tests for registration, login, profile retrieval, and token refresh.
POST /api/auth/register
- User registrationPOST /api/auth/login
- User loginGET /api/auth/me
- Get user profile (protected)POST /api/auth/refresh
- Refresh access tokenPOST /api/auth/logout
- Logout userGET /api/user/
- Get all users (admin only)GET /api/user/{id}
- Get user by IDPUT /api/user/{id}
- Update user by IDDELETE /api/user/{id}
- Delete user by ID (admin only)
This authentication system implements several security best practices:
- Password hashing with bcrypt
- JWT-based authentication
- Token expiration and refresh mechanism
- Role-based access control
- Protection against common security vulnerabilities
- Strong password validation
- Secure HTTP headers (in production)
-
PostgreSQL Connection Issues
- Make sure PostgreSQL is running and accessible
- Check that the port, username, and password match your configuration
-
Admin Access Issues
- If you can't access admin endpoints, make sure you created an admin user
- Verify that you're using the correct JWT token with admin privileges
- Visit
/login
to log in, or/signup
to register a new account. - After login, you'll be redirected to
/dashboard
where you can:- View your JWT tokens
- Refresh your access token
- View your user profile
- (If admin) View all users
- Logout
- POST /api/auth/register - User registration
- POST /api/auth/login - User login
- GET /api/auth/me - Get user profile (protected)
- POST /api/auth/refresh - Refresh access token
- POST /api/auth/logout - Logout user
- GET /api/user/ - Get all users (admin only)
- GET /api/user/{id} - Get user by ID
- PUT /api/user/{id} - Update user by ID
- DELETE /api/user/{id} - Delete user by ID (admin only)
This authentication system implements several security best practices:
- Password hashing with bcrypt
- JWT-based authentication
- Token expiration and refresh mechanism
- Role-based access control
- Protection against common security vulnerabilities
- Secure HTTP headers (in production)
- Modern, secure frontend authentication flows
- Removed Swagger UI and static API dashboard
- Added interactive login and signup pages
- Added a modern dashboard page for JWT and user management
- All authentication and user management can now be done through the web UI
- Admins can view all users from the dashboard
- JWT is managed in the frontend (localStorage for demo; can be upgraded to HttpOnly cookies)