feat(user): support login with any case of username & email
This commit is contained in:
12
app/auth.py
12
app/auth.py
@@ -111,13 +111,15 @@ async def authenticate_user_legacy(db: AsyncSession, name: str, password: str) -
|
||||
pw_md5 = hashlib.md5(password.encode()).hexdigest()
|
||||
|
||||
# 2. 根据用户名查找用户
|
||||
statement = select(User).where(User.username == name).options()
|
||||
user = (await db.exec(statement)).first()
|
||||
if not user:
|
||||
user = None
|
||||
user = (await db.exec(select(User).where(User.username == name))).first()
|
||||
if user is None:
|
||||
user = (await db.exec(select(User).where(User.email == name))).first()
|
||||
if user is None and name.isdigit():
|
||||
user = (await db.exec(select(User).where(User.id == int(name)))).first()
|
||||
if user is None:
|
||||
return None
|
||||
|
||||
await db.refresh(user)
|
||||
|
||||
# 3. 验证密码
|
||||
if user.pw_bcrypt is None or user.pw_bcrypt == "":
|
||||
return None
|
||||
|
||||
@@ -42,7 +42,7 @@ from fastapi import APIRouter, Depends, Form, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
from redis.asyncio import Redis
|
||||
from sqlalchemy import text
|
||||
from sqlmodel import select
|
||||
from sqlmodel import exists, select
|
||||
|
||||
|
||||
def create_oauth_error_response(error: str, description: str, hint: str, status_code: int = 400):
|
||||
@@ -101,12 +101,12 @@ async def register_user(
|
||||
email_errors = validate_email(user_email)
|
||||
password_errors = validate_password(user_password)
|
||||
|
||||
result = await db.exec(select(User).where(User.username == user_username))
|
||||
result = await db.exec(select(exists()).where(User.username == user_username))
|
||||
existing_user = result.first()
|
||||
if existing_user:
|
||||
username_errors.append("Username is already taken")
|
||||
|
||||
result = await db.exec(select(User).where(User.email == user_email))
|
||||
result = await db.exec(select(exists()).where(User.email == user_email))
|
||||
existing_email = result.first()
|
||||
if existing_email:
|
||||
email_errors.append("Email is already taken")
|
||||
|
||||
@@ -11,7 +11,7 @@ from app.utils import utcnow
|
||||
from .router import router
|
||||
|
||||
from fastapi import Body, HTTPException, Security
|
||||
from sqlmodel import select
|
||||
from sqlmodel import exists, select
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -34,7 +34,7 @@ async def user_rename(
|
||||
返回:
|
||||
- 成功: None
|
||||
"""
|
||||
samename_user = (await session.exec(select(User).where(User.username == new_name))).first()
|
||||
samename_user = (await session.exec(select(exists()).where(User.username == new_name))).first()
|
||||
if samename_user:
|
||||
raise HTTPException(409, "Username Exisits")
|
||||
errors = validate_username(new_name)
|
||||
|
||||
Reference in New Issue
Block a user