Project Name: Flask Authentication API
This project is a simple Flask-based API that provides user authentication and registration functionalities. It supports JWT-based authentication, user registration, and login with password hashing and verification.
- User Registration: Register new users with hashed passwords.
- User Login: Login users and generate a JWT token for authentication.
- JWT Authentication: Secure API access using JWT (JSON Web Tokens).
- Database Integration: Uses SQLAlchemy with MySQL (or SQLite) for persistent storage.
- Error Handling: Proper validation and error responses for registration and login requests.
- Backend: Flask
- Database: MySQL / SQLite
- Authentication: JWT (Flask-JWT-Extended)
- ORM: SQLAlchemy
- Environment Variables: Dotenv
- Password Hashing: Bcrypt (for secure password storage)
git clone https://github.com/Jhaveri-Jeet/Flask-API.git
cd Flask-API
Run the following command to create the virtual environment:
python3 -m venv path/to/your/virtualenv
Activate the virtual environment:
- On Windows:
path\to\your\virtualenv\Scripts\activate
- On macOS/Linux:
source path/to/your/virtualenv/bin/activate
Once the virtual environment is activated, install the required packages by running:
pip install -r requirements.txt
- Make sure your XAMPP server is running. You will need to start Apache and MySQL from the XAMPP control panel.
- Open phpMyAdmin (usually accessible via
http://localhost/phpmyadmin/
). - Create a new database, for example
flask_auth_db
. - Update your
.env
file with the correct database connection information:SECRET_KEY=your_secret_key SQLALCHEMY_DATABASE_URI=mysql+pymysql://username:password@localhost/flask_auth_db SQLALCHEMY_TRACK_MODIFICATIONS=False JWT_SECRET_KEY=your_jwt_secret_key
Run the following commands to create the necessary database tables:
python
>>> from app import db
>>> db.create_all()
Start the Flask application using:
flask run
By default, it will be accessible at http://localhost:5000
.
- Endpoint:
/user/register
- Method:
POST
- Request Body:
{ "name": "John Doe", "email": "johndoe@example.com", "password": "securePassword" }
- Response:
201 Created
: User successfully created.400 Bad Request
: Missing fields or invalid data.409 Conflict
: Email already exists.
- Endpoint:
/user/login
- Method:
POST
- Request Body:
{ "email": "johndoe@example.com", "password": "securePassword" }
- Response:
200 OK
: Successfully logged in, returns JWT token.401 Unauthorized
: Invalid credentials.
- Endpoint:
/user/profile
- Method:
GET
- Authentication: Bearer JWT token in the header.
- Response: User profile details if the token is valid.
id
: Integer (Primary Key)name
: Stringemail
: String (Unique)password_hash
: String (Hashed password)