onedrive/utils/odAuthTokenStore.ts

39 lines
1 KiB
TypeScript
Raw Normal View History

// This should be only used on the server side, where the tokens are stored with KV store using
// a file system based storage. The tokens are stored in the file system as JSON at /tmp path.
import os from 'os'
import Keyv from 'keyv'
2021-12-31 05:19:38 +00:00
// import { KeyvFile } from 'keyv-file'
2021-12-31 04:36:30 +00:00
console.log(`${os.tmpdir()}/od-auth-token.json`)
2021-12-31 05:19:38 +00:00
// const kv = new Keyv({
// store: new KeyvFile({
// filename: `${os.tmpdir()}/od-auth-token.json`,
// }),
// })
const kv = new Keyv({})
export async function storeOdAuthTokens({
accessToken,
accessTokenExpiry,
refreshToken,
}: {
accessToken: string
accessTokenExpiry: number
refreshToken: string
}): Promise<void> {
await kv.set('access_token', accessToken, accessTokenExpiry)
await kv.set('refresh_token', refreshToken)
}
export async function getOdAuthTokens(): Promise<{ accessToken: unknown; refreshToken: unknown }> {
const accessToken = await kv.get('access_token')
const refreshToken = await kv.get('refresh_token')
return {
accessToken,
refreshToken,
}
}