This Flask application provides a simple user management system with functionalities for creating users, authenticating them, and updating their information. It utilizes PostgreSQL for data storage, Flask-Migrate for database migrations, and bcrypt for password hashing.
Features: User registration with email and password Basic HTTP authentication for protected routes User information retrieval and update Health check endpoint for the application status
Requirements Python 3.x Flask Flask-SQLAlchemy Flask-Migrate Flask-HTTPAuth PostgreSQL bcrypt pytest
Setup and Installation:
- Clone the repository
git clone <repository_url> cd <repository_folder>
-
Install dependencies: pip install -r requirements.txt
-
Configure PostgreSQL: Ensure you have PostgreSQL installed and running on your machine. Create a database for the application and update the SQLALCHEMY_DATABASE_URI in the application's configuration with your database credentials.
Example:
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:password@localhost/database_name'
- Initialize the database: Run the following commands to create your database tables:
flask db init flask db migrate -m "Initial migration." flask db upgrade
- Run the application: flask run The application will start running on http://127.0.0.1:5000/. (Depends on what port you have specified, this is a general localhost for running the application)
API Endpoints: User Account Creation:
URL: /v1/user Method: POST
Payload: json
{ "email": "user@example.com", "password": "yourpassword", "first_name": "First", "last_name": "Last" } Response: User information with a 201 status code on success. User Login Basic HTTP Authentication is used for login. Use the registered email and password to access protected endpoints.
Get User Information URL: /v1/user/self Method: GET Authentication: Required
Update User Information: URL: /v1/user/self Method: PUT Payload: json
{ "first_name": "UpdatedFirst", "last_name": "UpdatedLast", "password": "newpassword" } Authentication: Required
Health Check URL: /healthz Method: GET
Error Handling The application uses standard HTTP response codes to indicate the success or failure of an API request.