sql_app: add username in regsiter

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-04-04 02:02:29 +03:00
parent 866182c25e
commit 3153a7db8d
5 changed files with 14 additions and 4 deletions

4
run-tls Executable file → Normal file
View File

@ -1,4 +1,4 @@
#!/bin/bash
uvicorn server:app --ssl-certfile server.crt --ssl-keyfile server.key --port 4433 --host 0.0.0.0 --no-server-header --reload
source venv/bin/activate
uvicorn sql_app.main:app --ssl-certfile server.crt --ssl-keyfile server.key --port 4433 --host 0.0.0.0 --no-server-header

View File

@ -12,6 +12,9 @@ def get_user(db: Session, user_id: int):
def get_user_by_email(db: Session, email: str):
return db.query(models.User).filter(models.User.email == email).first()
def get_user_by_username(db: Session, username: str):
return db.query(models.User).filter(models.User.username == username).first()
def get_users(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.User).offset(skip).limit(limit).all()
@ -22,7 +25,7 @@ def create_user(db: Session, user: schemas.UserCreate):
salt = key[1]
hashed_pass = key[0]
# TODO: check if user already exists? based on name,email ...
db_user = models.User(email=user.email, hashed_password=hashed_pass, passwd_salt=salt)
db_user = models.User(email=user.email, username=user.username,hashed_password=hashed_pass, passwd_salt=salt)
db.add(db_user)
db.commit()
db.refresh(db_user)

View File

@ -21,11 +21,16 @@ def get_db():
db.close()
@app.post("/users/", response_model=schemas.User)
@app.post("/users/reg", response_model=schemas.User)
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
db_user = crud.get_user_by_email(db, email=user.email)
if db_user:
raise HTTPException(status_code=400, detail="Email already registered")
db_user = crud.get_user_by_username(db, username=user.username)
if db_user:
raise HTTPException(status_code=400, detail="Username already registerd")
return crud.create_user(db=db, user=user)

View File

@ -9,6 +9,7 @@ class User(Base):
id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, index=True)
username = Column(String, unique=True, index=True)
hashed_password = Column(String)
passwd_salt = Column(String)
is_active = Column(Boolean, default=True)

View File

@ -22,6 +22,7 @@ class IotEntity(IotEntityBase):
class UserBase(BaseModel):
email: str
username: str
class UserCreate(UserBase):