mirror of
https://github.com/Nezumi-2711/onedrive-vercel-index.git
synced 2026-09-22 13:38:45 +00:00
store tokens in redis with upstash
This commit is contained in:
+1
-11
@@ -4,7 +4,6 @@ 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'
|
||||
@@ -15,13 +14,6 @@ import { getOdAuthTokens, storeOdAuthTokens } from '../../utils/odAuthTokenStore
|
||||
const basePath = pathPosix.resolve('/', siteConfig.baseDirectory)
|
||||
const clientSecret = revealObfuscatedToken(apiConfig.obfuscatedClientSecret)
|
||||
|
||||
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))
|
||||
if (encodedPath === '/' || encodedPath === '') {
|
||||
@@ -32,7 +24,7 @@ const encodePath = (path: string) => {
|
||||
}
|
||||
|
||||
async function getAccessToken(): Promise<any> {
|
||||
const { accessToken, refreshToken } = await getOdAuthTokens(keyv)
|
||||
const { accessToken, refreshToken } = await getOdAuthTokens()
|
||||
|
||||
// Return in storage access token if it is still valid
|
||||
if (typeof accessToken === 'string') {
|
||||
@@ -66,7 +58,6 @@ async function getAccessToken(): Promise<any> {
|
||||
accessToken: access_token,
|
||||
accessTokenExpiry: parseInt(expires_in),
|
||||
refreshToken: refresh_token,
|
||||
keyv,
|
||||
})
|
||||
console.log('Fetch new access token with stored refresh token.')
|
||||
return access_token
|
||||
@@ -91,7 +82,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
accessToken,
|
||||
accessTokenExpiry,
|
||||
refreshToken,
|
||||
keyv,
|
||||
})
|
||||
res.status(200).send('OK')
|
||||
return
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import Head from 'next/head'
|
||||
import router from 'next/router'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
import siteConfig from '../../config/site.json'
|
||||
import apiConfig from '../../config/api.json'
|
||||
@@ -8,6 +8,9 @@ import Footer from '../../components/Footer'
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||
|
||||
export default function OAuthStep1() {
|
||||
const redis_url = process.env.REDIS_URL
|
||||
const router = useRouter()
|
||||
|
||||
return (
|
||||
<div className="dark:bg-gray-900 flex flex-col items-center justify-center min-h-screen bg-white">
|
||||
<Head>
|
||||
@@ -29,6 +32,7 @@ export default function OAuthStep1() {
|
||||
</p>
|
||||
|
||||
<h3 className="font-medium text-lg mt-4 mb-2">Step 1/3: Preparations</h3>
|
||||
|
||||
<p className="py-1">
|
||||
Check the following configurations (especially <code className="text-sm font-mono">client_id</code> and{' '}
|
||||
<code className="text-sm font-mono">client_secret</code> (obfuscated)) and see if they match the official
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import Head from 'next/head'
|
||||
import router from 'next/router'
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||
|
||||
import siteConfig from '../../config/site.json'
|
||||
@@ -10,6 +10,8 @@ import { LoadingIcon } from '../../components/Loading'
|
||||
import { extractAuthCodeFromRedirected, generateAuthorisationUrl } from '../../utils/oAuthHandler'
|
||||
|
||||
export default function OAuthStep2() {
|
||||
const router = useRouter()
|
||||
|
||||
const [oAuthRedirectedUrl, setOAuthRedirectedUrl] = useState('')
|
||||
const [authCode, setAuthCode] = useState('')
|
||||
const [buttonLoading, setButtonLoading] = useState(false)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import router from 'next/router'
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
||||
|
||||
import siteConfig from '../../config/site.json'
|
||||
@@ -12,6 +12,8 @@ import { obfuscateToken, requestTokenWithAuthCode } from '../../utils/oAuthHandl
|
||||
import { LoadingIcon } from '../../components/Loading'
|
||||
|
||||
export default function OAuthStep3({ accessToken, expiryTime, refreshToken, error, description, errorUri }) {
|
||||
const router = useRouter()
|
||||
|
||||
const [buttonContent, setButtonContent] = useState(
|
||||
<>
|
||||
<span>Store tokens</span> <FontAwesomeIcon icon="key" />
|
||||
@@ -21,33 +23,42 @@ export default function OAuthStep3({ accessToken, expiryTime, refreshToken, erro
|
||||
const sendAuthTokensToServer = async () => {
|
||||
setButtonContent(
|
||||
<>
|
||||
<span>Store tokens</span> <LoadingIcon className="animate-spin w-4 h-4 ml-1 inline" />
|
||||
<span>Storing tokens</span> <LoadingIcon className="animate-spin w-4 h-4 ml-1 inline" />
|
||||
</>
|
||||
)
|
||||
|
||||
await axios.post(
|
||||
'/api',
|
||||
{
|
||||
obfuscatedAccessToken: obfuscateToken(accessToken),
|
||||
accessTokenExpiry: parseInt(expiryTime),
|
||||
obfuscatedRefreshToken: obfuscateToken(refreshToken),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
await axios
|
||||
.post(
|
||||
'/api',
|
||||
{
|
||||
obfuscatedAccessToken: obfuscateToken(accessToken),
|
||||
accessTokenExpiry: parseInt(expiryTime),
|
||||
obfuscatedRefreshToken: obfuscateToken(refreshToken),
|
||||
},
|
||||
}
|
||||
)
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}
|
||||
)
|
||||
.then(_ => {
|
||||
setButtonContent(
|
||||
<>
|
||||
<span>Stored! Going home...</span> <FontAwesomeIcon icon="check" />
|
||||
</>
|
||||
)
|
||||
|
||||
setButtonContent(
|
||||
<>
|
||||
<span>Stored! Going home...</span> <FontAwesomeIcon icon="check" />
|
||||
</>
|
||||
)
|
||||
|
||||
setTimeout(() => {
|
||||
router.push('/')
|
||||
}, 3000)
|
||||
setTimeout(() => {
|
||||
router.push('/')
|
||||
}, 3000)
|
||||
})
|
||||
.catch(_ => {
|
||||
setButtonContent(
|
||||
<>
|
||||
<span>Error storing the token</span> <FontAwesomeIcon icon="exclamation-circle" />
|
||||
</>
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user