This is a basic Flask application that demonstrates how to integrate MySQL, Flask-SQLAlchemy for database interaction, and JWT (JSON Web Token) for authentication. The application allows users to register, log in, and retrieve information about their subscription plans based on their roles.
- Clone the repository to your local machine:
git clone https://github.com/mscbuild/User_story_use-case.git2 Create a virtual environment (optional but recommended):
python -m venv venv3.Activate the virtual environment:
- Windows:
venv\Scripts\activate- MacOS/Linux:
source venv/bin/activate4.Install the required dependencies:
pip install -r requirements.txt5.You will need a MySQL database for the application. Follow the steps below to set it up.
-
Install MySQL on your local machine if you haven't already. You can download it from here.
-
Create a database named flask_db (or any name you prefer) and the following tables:
- Update the SQLALCHEMY_DATABASE_URI in app.py to reflect your MySQL credentials:
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://username:password@localhost/flask_db'- Run the application.
flask-mysql-jwt/
│
├── app.py # Main application file
├── requirements.txt # Python dependencies
├── README.md # Project documentation
└── venv/ # Virtual environment (optional)Register
POST /register
Registers a new user. The user will be added to the database with a hashed password.
Request Body:
{
"username": "john_doe",
"password": "password123",
"role": "premium"
}Response:
{
"message": "User registered successfully"
}Login
POST /login
Authenticates the user and generates a JWT access token.
Request Body:
{
"username": "john_doe",
"password": "password123"
}Response:
{
"access_token": "your_jwt_token"
}Get Tariff
GET /tariffs/{user_id}
Returns the current subscription plan for the user, based on their role. This endpoint requires JWT authentication.
Request Header:
Authorization: Bearer your_jwt_tokenResponse:
{
"userId": 1,
"tariffName": "Premium Plan",
"cost": 999.99,
"currency": "RUB",
"validity": "1 month",
"features": [
"Unlimited calls",
"10 GB internet",
"Free SMS"
]
}-
Make sure MySQL is running and your database is set up as described above.
-
Run the Flask application:
python app.py
- The application will be running at http://127.0.0.1:5000/.
- Use an API testing tool like Postman or cURL to interact with the API.
# 🔀 Example: Register a User
***POST /register***
~~~ruby
curl -X POST http://127.0.0.1:5000/register -H "Content-Type: application/json" -d '{"username": "john_doe", "password": "password123", "role": "premium"}'
POST /login
curl -X POST http://127.0.0.1:5000/login -H "Content-Type: application/json" -d '{"username": "john_doe", "password": "password123"}'GET /tariffs/1
curl -X GET http://127.0.0.1:5000/tariffs/1 -H "Authorization: Bearer your_jwt_token"We welcome contributions! If you have suggestions, improvements, or bug fixes, feel free to open an issue or submit a pull request.
-
Fork the repository.
-
Create a new branch (git checkout -b feature-name).
-
Commit your changes (git commit -am 'Add new feature').
-
Push to the branch (git push origin feature-name).
-
Open a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.