This is a Flask application demonstrating session management and user authentication.
- User login with username validation
- Session management using Flask-Session
- Profile page with visit counter
- Logout functionality
- Error handling (404 and 500 pages)
- Configurable settings for different environments (development, production, testing)
Here's a high-level overview of the application architecture:
session-management-flask-app/
│
├── src/
│ ├── forms/ # Directory for form-related files
│ │ └── login_form.py # Defines the LoginForm class for user authentication
│ ├── routes/ # Directory for route handlers
│ │ ├── auth_routes.py # Handles authentication-related routes (login, logout)
│ │ └── user_routes.py # Handles user-related routes (home, profile)
│ ├── static/ # Directory for static files
│ │ └── style.css # Contains CSS styles for the application
│ ├── templates/ # Directory for HTML templates
│ │ ├── 404.html # Template for 404 Not Found error page
│ │ ├── 500.html # Template for 500 Internal Server Error page
│ │ ├── base.html # Base template that other templates extend
│ │ ├── home.html # Template for the home page
│ │ ├── login.html # Template for the login page
│ │ └── profile.html # Template for the user profile page
│ ├── tests/ # Directory for test files
│ │ ├── init.py # Makes tests a Python package
│ │ └── test_app.py # Contains unit tests for the application
│ ├── utils/ # Directory for utility functions
│ │ └── cookie_handler.py # Provides functions for cookie management
│ ├── app.py # Main application file, sets up Flask app and configurations
│ └── config.py # Contains configuration classes for different environments
├── images/ # Directory for image files used in README
├── .gitattributes # Git attributes file
├── .gitignore # Git ignore file
├── LICENSE # MIT License file
├── README.md # This file
└── requirements.txt # Lists project dependencies
-
Clone the repository:
git clone https://github.com/spicy/session-management-flask-app.git cd session-management-flask-app
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
-
On Windows:
venv\Scripts\activate
-
On macOS and Linux:
source venv/bin/activate
-
-
Install dependencies:
pip install -r requirements.txt
The application uses different configuration classes for development, production, and testing environments. You can modify these in src/config.py
.
To set the environment, use the FLASK_ENV
environment variable:
- For development:
set FLASK_ENV=development
(Windows) orexport FLASK_ENV=development
(Unix) - For production:
set FLASK_ENV=production
(Windows) orexport FLASK_ENV=production
(Unix)
Ensure that the SECRET_KEY
is set in the environment variables.
- Example:
set SECRET_KEY=your_secret_key
(Windows) orexport SECRET_KEY=your_secret_key
(Unix)
-
Set the Flask application:
set FLASK_APP=src/app.py
(Windows) orexport FLASK_APP=src/app.py
(Unix) -
Run the application:
flask run
-
Open a web browser and navigate to
http://127.0.0.1:5000
- Navigate to the login page at
http://127.0.0.1:5000/login
- Enter a username in the format Firstname_Lastname (e.g., John_Doe)
- After successful login, you'll be redirected to the home page
- Click on "Profile" to view your profile and visit count
- Use the "Logout" link to end your session
To run the tests:
-
Ensure you're in the project root directory
-
Run the following command:
pytest src/tests
This will execute all the tests in the src/tests/
directory.
- CSRF protection using Flask-WTF
- Secure session management with Flask-Session
- HTTP-only cookies
- Configurable secure cookies for HTTPS
- Input validation for username
The application logs errors and important events to logs/flask_app.log
using a RotatingFileHandler. To view logs:
- Check the
logs/
directory in the project root - Open
flask_app.log
to view logged information
- Fork the repository
- Create a new branch:
git checkout -b feature-branch-name
- Make your changes and commit them:
git commit -m 'Add some feature'
- Push to the branch:
git push origin feature-branch-name
- Create a pull request
This project is licensed under the MIT License. See the LICENSE file for details.