JWT authenication in fastapi
This is a simple fastapi application that demonstrates how to use JWT for authentication. It has a simple user registration and login system. The user is authenticated using JWT and the token is used to access protected routes.
git clone 'repository url'
cd jwt_authentication_fastapi
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
uvicorn main:app --reload
The code is divided into 3 main parts:
- User model and database
- JWT authentication
- Routes
The user model is defined in the models.py
file. It is a simple model with 4 fields: id
, username
, fullname
and password
. The id
field is the primary key and is autoincremented. The username
field is a string and is unique. The password
field is also a string and is hashed using bcrypt
before being stored in the database.
class User(Base):
id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, index=True)
fullname = Column(String)
password = Column(String)
The model schemas as defined in the schemas.py
file. The UserCreate
schema is used for user registration and the UserResponse
schema is used to return the user details after registration and login. Token schema is used to return the JWT token after login.
from typing import List, Optional
class UserCreate(BaseModel):
username: str
full_name :str
password:str
class UserLogin(BaseModel):
username: str
password:str
class Token(BaseModel):
access_token:str
token_type:str
class UserResponse(BaseModel):
id:int
username:str
full_name:str
class Config:
orm_mode = True
The JWT authentication is implemented in the auth.py
file. The create_access_token
function is used to create the JWT token. The get_current_user
function is used to get the current user from the JWT token. The get_password_hash
function is used to hash the password before storing it in the database. The verify_password
function is used to verify the password during login.
Note that the SECRET_KEY
and ALGORITHM
are should be stored in a .env
file and not hardcoded in the code.
SECRET_KEY
ALGORITHM
ACCESS_TOKEN_EXPIRE_MINUTES
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def get_current_user(db:Session = Depends(get_db) , access_token:str = Depends(oauth2_schema)):
credentials_exception = JWTError
try:
payload = jwt.decode(access_token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
if username is None:
raise credentials_exception
except JWTError:
raise credentials_exception
user = db.query(User).filter(User.username == username).first()
if user is None:
raise credentials_exception
return user