1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use super::ApiKeyPlugin;
use better_auth_core::{
AuthContext, AuthResponse, AuthResult, AuthRoute, AuthSchema, FieldMap, FieldValue, HttpMethod,
NativeRequest, observability::LogArgument,
};
impl ApiKeyPlugin {
/// Run the registered server-only cleanup endpoint and retain its complete native result.
pub async fn delete_all_expired_api_keys(
&self,
context: &AuthContext<impl AuthSchema>,
source: NativeRequest<'_>,
body: FieldValue,
query: FieldValue,
) -> AuthResult<FieldValue> {
let response = context
.dispatch_native_value(
source,
AuthRoute::server_only(HttpMethod::Post, "deleteAllExpiredApiKeys"),
body,
query,
|_, context| async move {
let cleanup =
self.maybe_delete_expired(&context, true)
.await
.or_else(|error| {
context.logger().error(
"Failed to delete expired API keys:",
&[LogArgument::Error(&error)],
)
});
let (success, error) = match cleanup {
Ok(()) => (true, FieldValue::Null),
Err(error) => {
context.logger().error(
"[API KEY PLUGIN] Failed to delete expired API keys:",
&[LogArgument::Error(&error)],
)?;
(false, error.into_thrown_value()?)
}
};
Ok(AuthResponse::native(
None,
FieldMap::from([
("success".into(), success.into()),
("error".into(), error),
])
.into(),
))
},
)
.await?;
response.body.field_value()
}
}