Ease of use on web v0.0.3
This commit is contained in:
parent
86bd06f8ed
commit
66d721d8ed
22
api.py
22
api.py
@ -9,16 +9,10 @@ from tables import *
|
||||
|
||||
# fastapi init
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
origins = [
|
||||
"http://localhost:3000",
|
||||
"https://task.fstropii.com",
|
||||
"https://www.task.fstropii.com",
|
||||
"https://task.fstropii.com/",
|
||||
"http://localhost"
|
||||
]
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
@ -82,18 +76,18 @@ async def update_user(user: UserUpdate, session: SessionDep, current_user: User
|
||||
session.refresh(user_db)
|
||||
return user_db
|
||||
|
||||
@app.get('/user/login')
|
||||
@app.post('/user/login')
|
||||
async def verify_user(user: VerifyUser, session: SessionDep):
|
||||
existing_user = session.query(User).filter(User.email == user.email).first()
|
||||
existing_user = session.query(User).filter(User.username == user.username).first()
|
||||
if not existing_user:
|
||||
raise HTTPException(status_code=400, detail="Email not registered")
|
||||
return {"detail": "User not found"}
|
||||
|
||||
is_password_valid = validate_password(user.password, existing_user.password, existing_user.salt)
|
||||
|
||||
if not is_password_valid:
|
||||
raise HTTPException(status_code=400, detail="Password not valid")
|
||||
return {"detail": "Invalid password"}
|
||||
|
||||
return {"is_password_valid": is_password_valid, "email": existing_user.email, "id": existing_user.id}
|
||||
return {"detail": "Login successful"}
|
||||
|
||||
|
||||
@app.post('/token', response_model=Token)
|
||||
@ -104,7 +98,7 @@ async def login_for_access_token(session: SessionDep, form_data: OAuth2PasswordR
|
||||
|
||||
access_token_expires = timedelta(minutes=int(os.getenv("ACCESS_TOKEN_EXPIRES_MINUTES")))
|
||||
access_token = create_access_token(data={"sub": user.username}, expires_delta=access_token_expires)
|
||||
return {"access_token": access_token, "token_type": "bearer"}
|
||||
return {"access_token": access_token, "token_type": "bearer", "expires_days": access_token_expires.days}
|
||||
|
||||
@app.get("/users/me/", response_model=UserPublic)
|
||||
async def read_users_me(session: SessionDep, current_user: User = Depends(get_current_user)):
|
||||
|
@ -6,6 +6,7 @@ from sqlalchemy import Column, JSON
|
||||
class Token(BaseModel):
|
||||
access_token: str
|
||||
token_type: str
|
||||
expires_days: int
|
||||
|
||||
class TokenData(BaseModel):
|
||||
username: str | None = None
|
||||
@ -53,5 +54,5 @@ class UserUpdate(SQLModel):
|
||||
is_active: bool | None = None
|
||||
|
||||
class VerifyUser(BaseModel):
|
||||
email: str
|
||||
username: str
|
||||
password: str
|
Loading…
Reference in New Issue
Block a user