Gregory Schier
2025-05-30 08:02:29 -07:00
parent 1e27e1d8cb
commit b52570bf58
8 changed files with 126 additions and 54 deletions

View File

@@ -1,8 +1,13 @@
import { Context } from '@yaakapp/api';
export async function storeToken(ctx: Context, contextId: string, response: AccessTokenRawResponse) {
if (!response.access_token) {
throw new Error(`Token not found in response`);
export async function storeToken(
ctx: Context,
contextId: string,
response: AccessTokenRawResponse,
tokenName: 'access_token' | 'id_token' = 'access_token',
) {
if (!response[tokenName]) {
throw new Error(`${tokenName} not found in response ${Object.keys(response).join(', ')}`);
}
const expiresAt = response.expires_in ? Date.now() + response.expires_in * 1000 : null;
@@ -41,12 +46,13 @@ function dataDirStoreKey(context_id: string) {
}
export interface AccessToken {
response: AccessTokenRawResponse,
response: AccessTokenRawResponse;
expiresAt: number | null;
}
export interface AccessTokenRawResponse {
access_token: string;
id_token?: string;
token_type?: string;
expires_in?: number;
refresh_token?: string;