feat(auth): 修复网页端登录流程,移除 web_generate_login_code 的 JWT 依赖
This commit is contained in:
@@ -368,12 +368,10 @@ pub struct WebLoginCodeResponse {
|
||||
pub async fn web_generate_login_code(
|
||||
pool: web::Data<PgPool>,
|
||||
req: web::Json<WebLoginCodeRequest>,
|
||||
claims: web::ReqData<Claims>,
|
||||
http_client: web::Data<Client>,
|
||||
app_state: web::Data<AppState>,
|
||||
) -> impl Responder {
|
||||
let code = req.code.trim();
|
||||
let user_id = claims.user_id; // 从 JWT 获取当前用户
|
||||
|
||||
// 1. 用 code 换取 openid(和登录流程一样)
|
||||
let url = format!(
|
||||
@@ -414,7 +412,28 @@ pub async fn web_generate_login_code(
|
||||
}
|
||||
};
|
||||
|
||||
// 2. 清理该 openid 的旧登录码(避免重复使用)
|
||||
// 2. 通过 openid 查询或创建用户(不依赖 JWT)
|
||||
let user_id: i32 = match sqlx::query_as::<_, (i32,)>(
|
||||
r#"
|
||||
INSERT INTO users (openid, name, type)
|
||||
VALUES ($1, left($1, 8), 2)
|
||||
ON CONFLICT (openid) DO UPDATE SET id = users.id
|
||||
RETURNING id
|
||||
"#,
|
||||
)
|
||||
.bind(&openid)
|
||||
.fetch_one(pool.get_ref())
|
||||
.await
|
||||
{
|
||||
Ok((id,)) => id,
|
||||
Err(e) => {
|
||||
error!("用户查询/创建失败: {}", e);
|
||||
return HttpResponse::InternalServerError()
|
||||
.json(ErrorResponse::<()>::error("用户处理失败"));
|
||||
}
|
||||
};
|
||||
|
||||
// 3. 清理该 openid 的旧登录码(避免重复使用)
|
||||
if let Err(e) = sqlx::query("DELETE FROM web_login_codes WHERE openid = $1")
|
||||
.bind(&openid)
|
||||
.execute(pool.get_ref())
|
||||
@@ -579,11 +598,13 @@ pub async fn web_login_confirm(
|
||||
_ => (false, None),
|
||||
};
|
||||
|
||||
// 删除已使用的登录码
|
||||
let _ = sqlx::query("DELETE FROM web_login_codes WHERE code = $1")
|
||||
// 更新登录码记录,设置 token(而非删除,让轮询接口能查到)
|
||||
sqlx::query("UPDATE web_login_codes SET token = $1 WHERE code = $2")
|
||||
.bind(&token)
|
||||
.bind(&code)
|
||||
.execute(pool.get_ref())
|
||||
.await;
|
||||
.await
|
||||
.ok();
|
||||
|
||||
info!("[WEB LOGIN CONFIRM] user_id={} is_paid={}", user_id, is_paid_active);
|
||||
HttpResponse::Ok().json(WebLoginConfirmResponse {
|
||||
|
||||
Reference in New Issue
Block a user