Skip to content

Commit a34c3f0

Browse files
committed
🚧 ZwiCo
1 parent abbfe02 commit a34c3f0

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/util/token-util.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { infoLogger, tokenCache } from '..';
1+
import { infoLogger, PubSubClient, tokenCache } from '..';
22
import { TokenCacheStorage } from '../cache';
33
import { MemoryStorageAdapter, RedisStorageAdapter } from '../cache/storage';
44
import { Config, ServerError } from '../models';
55
import { Token } from '../models/token.model';
6+
import assert from 'node:assert';
67

78
const REFRESH_MARKER_TTL = 5;
89
const DEFAULT_TTL = 3540;
@@ -116,3 +117,29 @@ export async function getFreshAccessToken(
116117

117118
return newToken.accessToken;
118119
}
120+
121+
export async function updateToken(
122+
config: Config,
123+
accessToken: string,
124+
providerKey: string,
125+
) {
126+
if (!tokenCache) {
127+
throw new ServerError(
128+
500,
129+
'Tried getting token from cache while cache was undefined.',
130+
);
131+
}
132+
const newToken = { accessToken, isPending: false };
133+
await tokenCache.set(providerKey, newToken, getTokenCacheTtl());
134+
const { PUBSUB_TOPIC_NAME_UPDATE_PROVIDER_KEY } = process.env;
135+
assert(
136+
PUBSUB_TOPIC_NAME_UPDATE_PROVIDER_KEY,
137+
'PUBSUB_TOPIC_NAME_UPDATE_PROVIDER_KEY is not defined',
138+
);
139+
const pubSubClient = new PubSubClient(PUBSUB_TOPIC_NAME_UPDATE_PROVIDER_KEY);
140+
await pubSubClient.publishMessage({
141+
userID: config.userId,
142+
providerKey,
143+
accessToken,
144+
});
145+
}

0 commit comments

Comments
 (0)