feat: record daily login tracking on auth success

This commit is contained in:
2026-05-08 14:00:04 +08:00
parent 98be6eb0e4
commit f343555a19
6 changed files with 146 additions and 2 deletions

View File

@@ -26,6 +26,47 @@ pub fn create_password_auth_session(
create_auth_session(state, user, session_client, AuthLoginMethod::Password)
}
#[cfg(not(test))]
pub async fn record_daily_login_tracking_event_after_auth_success(
state: &AppState,
request_context: &crate::request_context::RequestContext,
user_id: &str,
login_method: AuthLoginMethod,
) {
// 登录埋点是运营数据,不应反向阻断已经成功的认证会话签发。
match state
.spacetime_client()
.record_daily_login_tracking_event(user_id.to_string())
.await
{
Ok(()) => tracing::info!(
request_id = request_context.request_id(),
operation = request_context.operation(),
user_id = %user_id,
login_method = %login_method.as_str(),
"登录成功每日登录埋点已记录"
),
Err(error) => tracing::warn!(
request_id = request_context.request_id(),
operation = request_context.operation(),
user_id = %user_id,
login_method = %login_method.as_str(),
error = %error,
"登录成功每日登录埋点记录失败,登录流程继续"
),
}
}
#[cfg(test)]
pub async fn record_daily_login_tracking_event_after_auth_success(
_state: &AppState,
_request_context: &crate::request_context::RequestContext,
_user_id: &str,
_login_method: AuthLoginMethod,
) {
// 单元测试默认不启动 SpacetimeDB这里仅验证登录链路调用点能通过编译并保持非阻断语义。
}
pub fn create_auth_session(
state: &AppState,
user: &AuthUser,

View File

@@ -4,7 +4,7 @@ use axum::{
http::{HeaderMap, StatusCode},
response::IntoResponse,
};
use module_auth::{PasswordEntryError, PasswordEntryInput};
use module_auth::{AuthLoginMethod, PasswordEntryError, PasswordEntryInput};
use serde_json::json;
use shared_contracts::auth::{PasswordEntryRequest, PasswordEntryResponse};
@@ -12,7 +12,8 @@ use crate::{
api_response::json_success_body,
auth_payload::map_auth_user_payload,
auth_session::{
attach_set_cookie_header, build_refresh_session_cookie_header, create_password_auth_session,
attach_set_cookie_header, build_refresh_session_cookie_header,
create_password_auth_session, record_daily_login_tracking_event_after_auth_success,
},
http_error::AppError,
request_context::RequestContext,
@@ -49,6 +50,13 @@ pub async fn password_entry(
}
let session_client = resolve_session_client_context(&headers);
let signed_session = create_password_auth_session(&state, &result.user, &session_client)?;
record_daily_login_tracking_event_after_auth_success(
&state,
&request_context,
&result.user.id,
AuthLoginMethod::Password,
)
.await;
state
.sync_auth_store_snapshot_to_spacetime()
.await

View File

@@ -16,6 +16,7 @@ use crate::{
auth_payload::map_auth_user_payload,
auth_session::{
attach_set_cookie_header, build_refresh_session_cookie_header, create_auth_session,
record_daily_login_tracking_event_after_auth_success,
},
http_error::AppError,
phone_auth::map_phone_auth_error,
@@ -79,6 +80,13 @@ pub async fn reset_password(
&session_client,
module_auth::AuthLoginMethod::Password,
)?;
record_daily_login_tracking_event_after_auth_success(
&state,
&request_context,
&result.user.id,
module_auth::AuthLoginMethod::Password,
)
.await;
let mut headers = HeaderMap::new();
attach_set_cookie_header(

View File

@@ -20,6 +20,7 @@ use crate::{
auth_payload::map_auth_user_payload,
auth_session::{
attach_set_cookie_header, build_refresh_session_cookie_header, create_auth_session,
record_daily_login_tracking_event_after_auth_success,
},
http_error::AppError,
platform_errors::{attach_retry_after, map_phone_auth_platform_store_error},
@@ -176,6 +177,13 @@ pub async fn phone_login(
&session_client,
AuthLoginMethod::Phone,
)?;
record_daily_login_tracking_event_after_auth_success(
&state,
&request_context,
&result.user.id,
AuthLoginMethod::Phone,
)
.await;
state
.sync_auth_store_snapshot_to_spacetime()
.await

View File

@@ -21,6 +21,7 @@ use crate::{
auth_payload::map_auth_user_payload,
auth_session::{
attach_set_cookie_header, build_refresh_session_cookie_header, create_auth_session,
record_daily_login_tracking_event_after_auth_success,
},
http_error::AppError,
platform_errors::{attach_retry_after, map_wechat_provider_error},
@@ -74,6 +75,7 @@ pub async fn start_wechat_login(
pub async fn handle_wechat_callback(
State(state): State<AppState>,
Extension(request_context): Extension<RequestContext>,
headers: HeaderMap,
Query(query): Query<WechatCallbackQuery>,
) -> Result<impl IntoResponse, AppError> {
@@ -141,6 +143,13 @@ pub async fn handle_wechat_callback(
&session_client,
AuthLoginMethod::Wechat,
)?;
record_daily_login_tracking_event_after_auth_success(
&state,
&request_context,
&result.user.id,
AuthLoginMethod::Wechat,
)
.await;
state
.sync_auth_store_snapshot_to_spacetime()
.await
@@ -208,6 +217,13 @@ pub async fn bind_wechat_phone(
&session_client,
AuthLoginMethod::Wechat,
)?;
record_daily_login_tracking_event_after_auth_success(
&state,
&request_context,
&result.user.id,
AuthLoginMethod::Wechat,
)
.await;
state
.sync_auth_store_snapshot_to_spacetime()
.await