feat: 定期刷新要过期的oauth授权用户

This commit is contained in:
Pan Qiancheng 2025-10-23 15:30:13 +08:00
parent f1e7894e48
commit 91ff162908
2 changed files with 55 additions and 1 deletions

View File

@ -3,8 +3,9 @@ import { Watcher } from 'oak-domain/lib/types';
import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
import token from './token';
import extraFile from './extraFile';
import oauth from './oauth';
export default [...token, ...extraFile] as Watcher<
export default [...token, ...extraFile, ...oauth] as Watcher<
EntityDict,
keyof EntityDict,
BackendRuntimeContext<EntityDict>

53
src/watchers/oauth.ts Normal file
View File

@ -0,0 +1,53 @@
import { EntityDict } from '../oak-app-domain';
import { BackendRuntimeContext } from '../context/BackendRuntimeContext';
import { Watcher, BBWatcher } from 'oak-domain/lib/types/Watcher';
const watchers: Watcher<
EntityDict,
'oauthUser',
BackendRuntimeContext<EntityDict>
>[] = [
{
name: '定期刷新过期的oauthUser令牌',
entity: 'oauthUser',
filter() {
const now = Date.now();
const tenMinutes = 10 * 60 * 1000; // 10分钟
const oneDay = 24 * 60 * 60 * 1000; // 1天
return {
$and: [
{
refreshToken: {
$exists: true,
},
},
{
state: {
provider: {
refreshEndpoint: {
$exists: true,
}
}
}
},
// accessToken在10分钟内过期包括已过期
{
accessExpiresAt: {
$lt: now + tenMinutes,
},
},
// refreshToken还没过期至少还有1天有效期
{
refreshExpiresAt: {
$gt: now + oneDay,
},
},
],
};
},
action: 'refreshTokens',
actionData: {},
},
];
export default watchers;