🐛 fix(auth.py): add optional parameter 'update_last_login' to 'create_user_tokens' function to allow updating last login only when needed

🐛 fix(auth.py): add optional parameter 'db' to 'create_refresh_token' function to allow passing the database session
✨ feat(login.py): pass 'update_last_login=True' to 'create_user_tokens' function to update last login when user successfully logs in
🐛 fix(ApiModal/index.tsx): fix formatting of 'codesArray' array to improve readability
🐛 fix(formModal/index.tsx): fix formatting of 'getWebSocketUrl' function to improve readability
🐛 fix(vite.config.ts): fix typo in comment
This commit is contained in:
gustavoschaedler 2023-08-15 16:50:40 +01:00
commit c752872fc3
6 changed files with 110 additions and 108 deletions

View file

@ -76,7 +76,9 @@ def create_token(data: dict, expires_delta: timedelta):
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
def create_user_tokens(user_id: UUID, db: Session = Depends(get_session)) -> dict:
def create_user_tokens(
user_id: UUID, db: Session = Depends(get_session), update_last_login: bool = False
) -> dict:
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_token(
data={"sub": str(user_id)},
@ -90,7 +92,8 @@ def create_user_tokens(user_id: UUID, db: Session = Depends(get_session)) -> dic
)
# Update: last_login_at
update_user_last_login_at(user_id, db)
if update_last_login:
update_user_last_login_at(user_id, db)
return {
"access_token": access_token,
@ -99,7 +102,7 @@ def create_user_tokens(user_id: UUID, db: Session = Depends(get_session)) -> dic
}
def create_refresh_token(refresh_token: str):
def create_refresh_token(refresh_token: str, db: Session = Depends(get_session)):
try:
payload = jwt.decode(refresh_token, SECRET_KEY, algorithms=[ALGORITHM])
user_id: UUID = payload.get("sub") # type: ignore
@ -110,7 +113,7 @@ def create_refresh_token(refresh_token: str):
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid refresh token"
)
return create_user_tokens(user_id)
return create_user_tokens(user_id, db)
except JWTError as e:
raise HTTPException(

View file

@ -20,7 +20,7 @@ async def login_to_get_access_token(
# _: Session = Depends(get_current_active_user)
):
if user := authenticate_user(form_data.username, form_data.password, db):
return create_user_tokens(user_id=user.id, db=db)
return create_user_tokens(user_id=user.id, db=db, update_last_login=True)
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,

View file

@ -49,7 +49,13 @@ const ApiModal = forwardRef(
const pythonCode = getPythonCode(flow, tweak.current, tabsState);
const widgetCode = getWidgetCode(flow, tabsState);
const tweaksCode = buildTweaks(flow);
const codesArray = [curl_code, pythonApiCode, pythonCode, widgetCode, pythonCode];
const codesArray = [
curl_code,
pythonApiCode,
pythonCode,
widgetCode,
pythonCode,
];
const [tabs, setTabs] = useState(tabsArray(codesArray, 0));
function startState() {

View file

@ -160,7 +160,8 @@ export default function FormModal({
}
function getWebSocketUrl(chatId, isDevelopment = false) {
const isSecureProtocol = window.location.protocol === "https:" || window.location.port === "443";
const isSecureProtocol =
window.location.protocol === "https:" || window.location.port === "443";
const webSocketProtocol = isSecureProtocol ? "wss" : "ws";
const host = isDevelopment ? "localhost:7860" : window.location.host;
const chatEndpoint = `/api/v1/chat/${chatId}`;

View file

@ -6,7 +6,7 @@ const apiRoutes = ["^/api/v1/", "/health"];
// Use environment variable to determine the target.
const target = process.env.VITE_PROXY_TARGET || "http://127.0.0.1:7860";
// Use environment variable to determine the UI server port
// Use environment variable to determine the UI server port
const port = process.env.VITE_PORT || 3000;
const proxyTargets = apiRoutes.reduce((proxyObj, route) => {