User Authentication App with Flask This is a simple user authentication application built with Flask, SQLAlchemy, and Flask-Bcrypt. It provides basic features such as user registration, login, and logout. This README will guide you through setting up the app, testing it, and integrating it with your main project.
Table of Contents Prerequisites Installation Setting Up the Application Running the Application API Endpoints Testing the Application Integrating with Your Main Project License Prerequisites Before you begin, ensure you have the following installed:
Python 3.6 or later pip (Python package installer) A code editor (e.g., VSCode, PyCharm) Installation Clone the repository:
bash Copy code git clone https://github.com/yourusername/react-flask-auth.git cd react-flask-auth/backend Create a virtual environment:
bash Copy code python -m venv venv Activate the virtual environment:
For macOS/Linux: bash Copy code source venv/bin/activate For Windows: bash Copy code venv\Scripts\activate Install the required packages:
bash Copy code pip install Flask Flask-SQLAlchemy Flask-Migrate Flask-Bcrypt Flask-Cors Initialize the database:
bash Copy code flask db init flask db migrate -m "Initial migration." flask db upgrade Setting Up the Application Configure the application:
Edit the app.py file and ensure the database URI and secret key are set according to your requirements.
Create a database:
This can be done automatically when you run the migration commands above.
Running the Application Start the Flask application:
bash Copy code flask run The application will be running at http://127.0.0.1:5000/.
API Endpoints The following endpoints are available for user authentication:
POST /register: Register a new user. Body: { "username": "your_username", "password": "your_password" } POST /login: Log in a user. Body: { "username": "your_username", "password": "your_password" } GET /logout: Log out the current user. Example Request with cURL bash Copy code curl -X POST http://127.0.0.1:5000/register -H "Content-Type: application/json" -d '{"username": "testuser", "password": "testpass"}' Testing the Application You can test the API using tools like Postman or cURL. Ensure that the Flask server is running before making any requests.
Register a new user. Log in with the registered user. Log out. Integrating with Your Main Project To integrate this authentication app into your main project:
Import the User model and any relevant functions from this app into your main project. Use the API endpoints in your main application to manage user authentication. Ensure your main project can communicate with this Flask app, either by running them on the same server or handling cross-origin requests if they are on different domains.