Add is_admin attribute on User model

master
Alix JEUDI--LEMOINE 1 week ago
parent 5b4615da67
commit 597cef0c22

@ -4,3 +4,4 @@ class Token(BaseModel):
access_token: str access_token: str
token_type: str token_type: str
user_id: str user_id: str
is_admin: bool

@ -5,3 +5,4 @@ class User(BaseModel):
uid: str = Field(..., alias="_id") uid: str = Field(..., alias="_id")
username: str username: str
password: str password: str
is_admin: bool = False

@ -34,12 +34,27 @@ async def register(user: UserRegisterDTO):
) )
hashed_password = get_password_hash(user.password) hashed_password = get_password_hash(user.password)
user_id = users_collection.insert_one({"username": user.username, "password": hashed_password}) user_id = users_collection.insert_one({
"username": user.username,
"password": hashed_password,
"is_admin": False
})
access_token_expires = timedelta(minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES) access_token_expires = timedelta(minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token(data={"sub": user.username}, expires_delta=access_token_expires) access_token = create_access_token(
data={
"sub": user.username,
"is_admin": False
},
expires_delta=access_token_expires
)
return {"access_token": access_token, "token_type": "bearer", "user_id": str(user_id.inserted_id)} return {
"access_token": access_token,
"token_type": "bearer",
"user_id": str(user_id.inserted_id),
"is_admin": False
}
@auth_router.post( @auth_router.post(
path="/login", path="/login",
@ -56,10 +71,20 @@ async def login(form_data: OAuth2PasswordRequestForm = Depends()):
) )
access_token_expires = timedelta(minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES) access_token_expires = timedelta(minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token(data={"sub": form_data.username}, expires_delta=access_token_expires) access_token = create_access_token(
data={
return {"access_token": access_token, "token_type": "bearer", "user_id": str(user["_id"])} "sub": form_data.username,
"is_admin": user.get("is_admin", False)
},
expires_delta=access_token_expires
)
return {
"access_token": access_token,
"token_type": "bearer",
"user_id": str(user["_id"]),
"is_admin": user.get("is_admin", False)
}
@auth_router.get( @auth_router.get(
path="/logout", path="/logout",

@ -15,5 +15,6 @@ def user_serialize(user) -> User:
return User( return User(
_id=str(user['_id']), _id=str(user['_id']),
username=user['username'], username=user['username'],
password=user['password'] password=user['password'],
is_admin=user['is_admin']
) )

@ -55,12 +55,14 @@ def populate_data():
# Créer les utilisateurs # Créer les utilisateurs
user1_id = users_collection.insert_one({ user1_id = users_collection.insert_one({
"username": "string", "username": "string",
"password": get_password_hash("string") "password": get_password_hash("string"),
"is_admin": True
}).inserted_id }).inserted_id
user2_id = users_collection.insert_one({ user2_id = users_collection.insert_one({
"username": "test", "username": "test",
"password": get_password_hash("test") "password": get_password_hash("test"),
"is_admin": False
}).inserted_id }).inserted_id
# Créer d'abord les pins # Créer d'abord les pins

Loading…
Cancel
Save