Fix login redirect by setting auth cookie and routing by role
Browse files- qa_annotate/api/user.py +10 -13
- qa_annotate/static/js/auth.js +3 -3
qa_annotate/api/user.py
CHANGED
|
@@ -70,9 +70,7 @@ def register(user_register: UserRegister, db: Session = Depends(get_db)):
|
|
| 70 |
|
| 71 |
|
| 72 |
@router.post("/login", response_model=Token)
|
| 73 |
-
def login(
|
| 74 |
-
user_login: UserLogin, db: Session = Depends(get_db), response: Response = None
|
| 75 |
-
):
|
| 76 |
"""用户登录"""
|
| 77 |
# 先检查用户是否存在
|
| 78 |
user = UserCRUD.authenticate_user(
|
|
@@ -113,16 +111,15 @@ def login(
|
|
| 113 |
)
|
| 114 |
|
| 115 |
# 设置cookie: 从配置读取过期时间,HttpOnly
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
)
|
| 126 |
|
| 127 |
return Token(access_token=access_token, token_type="bearer", user=user)
|
| 128 |
|
|
|
|
| 70 |
|
| 71 |
|
| 72 |
@router.post("/login", response_model=Token)
|
| 73 |
+
def login(user_login: UserLogin, response: Response, db: Session = Depends(get_db)):
|
|
|
|
|
|
|
| 74 |
"""用户登录"""
|
| 75 |
# 先检查用户是否存在
|
| 76 |
user = UserCRUD.authenticate_user(
|
|
|
|
| 111 |
)
|
| 112 |
|
| 113 |
# 设置cookie: 从配置读取过期时间,HttpOnly
|
| 114 |
+
response.set_cookie(
|
| 115 |
+
key="access_token",
|
| 116 |
+
value=access_token,
|
| 117 |
+
max_age=token_expire_seconds,
|
| 118 |
+
expires=token_expire_seconds,
|
| 119 |
+
path="/",
|
| 120 |
+
httponly=True,
|
| 121 |
+
samesite="lax",
|
| 122 |
+
)
|
|
|
|
| 123 |
|
| 124 |
return Token(access_token=access_token, token_type="bearer", user=user)
|
| 125 |
|
qa_annotate/static/js/auth.js
CHANGED
|
@@ -111,11 +111,11 @@ document.getElementById('loginForm').addEventListener('submit', async (e) => {
|
|
| 111 |
// 2秒后跳转
|
| 112 |
setTimeout(() => {
|
| 113 |
if (redirectUrl) {
|
| 114 |
-
// 如果有redirect参数,跳转到指定页面
|
| 115 |
window.location.href = decodeURIComponent(redirectUrl);
|
|
|
|
|
|
|
| 116 |
} else {
|
| 117 |
-
|
| 118 |
-
window.location.href = '/';
|
| 119 |
}
|
| 120 |
}, 2000);
|
| 121 |
} catch (error) {
|
|
|
|
| 111 |
// 2秒后跳转
|
| 112 |
setTimeout(() => {
|
| 113 |
if (redirectUrl) {
|
|
|
|
| 114 |
window.location.href = decodeURIComponent(redirectUrl);
|
| 115 |
+
} else if (data.user.is_superuser) {
|
| 116 |
+
window.location.href = '/manager';
|
| 117 |
} else {
|
| 118 |
+
window.location.href = '/user';
|
|
|
|
| 119 |
}
|
| 120 |
}, 2000);
|
| 121 |
} catch (error) {
|