mirror of
https://github.com/Nezumi-2711/onedrive-vercel-index.git
synced 2026-09-22 20:01:46 +00:00
store access token and refresh token in serverless mem
This commit is contained in:
+32
-24
@@ -1,38 +1,46 @@
|
||||
// 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 os from 'os'
|
||||
|
||||
import Keyv from 'keyv'
|
||||
// import { KeyvFile } from 'keyv-file'
|
||||
|
||||
console.log(`${os.tmpdir()}/od-auth-token.json`)
|
||||
// console.log(`${os.tmpdir()}/od-auth-token.json`)
|
||||
|
||||
// const kv = new Keyv({
|
||||
// store: new KeyvFile({
|
||||
// filename: `${os.tmpdir()}/od-auth-token.json`,
|
||||
// }),
|
||||
// })
|
||||
const kv = new Keyv({})
|
||||
export default class TokenStore {
|
||||
kv: 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)
|
||||
}
|
||||
constructor() {
|
||||
this.kv = new Keyv()
|
||||
|
||||
export async function getOdAuthTokens(): Promise<{ accessToken: unknown; refreshToken: unknown }> {
|
||||
const accessToken = await kv.get('access_token')
|
||||
const refreshToken = await kv.get('refresh_token')
|
||||
// this.kv = new Keyv({
|
||||
// store: new KeyvFile({
|
||||
// filename: `${os.tmpdir()}/od-auth-token.json`,
|
||||
// }),
|
||||
// })
|
||||
console.log('TokenStore constructor')
|
||||
}
|
||||
|
||||
return {
|
||||
async getOdAuthTokens(): Promise<{ accessToken: unknown; refreshToken: unknown }> {
|
||||
const accessToken = await this.kv.get('access_token')
|
||||
const refreshToken = await this.kv.get('refresh_token')
|
||||
|
||||
return {
|
||||
accessToken,
|
||||
refreshToken,
|
||||
}
|
||||
}
|
||||
|
||||
async storeOdAuthTokens({
|
||||
accessToken,
|
||||
accessTokenExpiry,
|
||||
refreshToken,
|
||||
}: {
|
||||
accessToken: string
|
||||
accessTokenExpiry: number
|
||||
refreshToken: string
|
||||
}): Promise<void> {
|
||||
await this.kv.set('access_token', accessToken, accessTokenExpiry)
|
||||
await this.kv.set('refresh_token', refreshToken)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user