mirror of
https://github.com/Nezumi-2711/onedrive-vercel-index.git
synced 2026-09-22 13:38:45 +00:00
lets try storing as tmp file again
This commit is contained in:
+16
-6
@@ -1,17 +1,26 @@
|
||||
import os from 'os'
|
||||
import { posix as pathPosix } from 'path'
|
||||
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
import axios from 'axios'
|
||||
import Keyv from 'keyv'
|
||||
import { KeyvFile } from 'keyv-file'
|
||||
|
||||
import apiConfig from '../../config/api.json'
|
||||
import siteConfig from '../../config/site.json'
|
||||
import { revealObfuscatedToken } from '../../utils/oAuthHandler'
|
||||
import { compareHashedToken } from '../../utils/protectedRouteHandler'
|
||||
import TokenStore from '../../utils/odAuthTokenStore'
|
||||
import { getOdAuthTokens, storeOdAuthTokens } from '../../utils/odAuthTokenStore'
|
||||
|
||||
const basePath = pathPosix.resolve('/', siteConfig.baseDirectory)
|
||||
const clientSecret = revealObfuscatedToken(apiConfig.obfuscatedClientSecret)
|
||||
const tokenStore = new TokenStore()
|
||||
|
||||
const keyv = new Keyv({
|
||||
store: new KeyvFile({
|
||||
filename: `${os.tmpdir()}/od-auth-token.json`,
|
||||
}),
|
||||
})
|
||||
console.log(`kv init - ${os.tmpdir()}/od-auth-token.json`)
|
||||
|
||||
const encodePath = (path: string) => {
|
||||
let encodedPath = pathPosix.join(basePath, pathPosix.resolve('/', path))
|
||||
@@ -23,8 +32,7 @@ const encodePath = (path: string) => {
|
||||
}
|
||||
|
||||
async function getAccessToken(): Promise<any> {
|
||||
const { accessToken, refreshToken } = await tokenStore.getOdAuthTokens()
|
||||
console.log(accessToken, refreshToken)
|
||||
const { accessToken, refreshToken } = await getOdAuthTokens(keyv)
|
||||
|
||||
// Return in storage access token if it is still valid
|
||||
if (typeof accessToken === 'string') {
|
||||
@@ -54,10 +62,11 @@ async function getAccessToken(): Promise<any> {
|
||||
|
||||
if ('access_token' in resp.data && 'refresh_token' in resp.data) {
|
||||
const { expires_in, access_token, refresh_token } = resp.data
|
||||
await tokenStore.storeOdAuthTokens({
|
||||
await storeOdAuthTokens({
|
||||
accessToken: access_token,
|
||||
accessTokenExpiry: parseInt(expires_in),
|
||||
refreshToken: refresh_token,
|
||||
keyv,
|
||||
})
|
||||
console.log('Fetch new access token with stored refresh token.')
|
||||
return access_token
|
||||
@@ -78,10 +87,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
return
|
||||
}
|
||||
|
||||
await tokenStore.storeOdAuthTokens({
|
||||
await storeOdAuthTokens({
|
||||
accessToken,
|
||||
accessTokenExpiry,
|
||||
refreshToken,
|
||||
keyv,
|
||||
})
|
||||
res.status(200).send('OK')
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user