Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
fix(oauth/store): ensure sessions are fresh (#166)
Browse files Browse the repository at this point in the history
* refactor(oauth/store): ensure sessions are fresh

* fix typos
  • Loading branch information
boywithkeyboard authored Aug 14, 2023
1 parent 52016f4 commit 1ca9bba
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions oauth/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,65 @@ export const kv = new OAuthStore({
if (c.runtime === 'cloudflare') {
const kv = (c.__app.env as { oauth: KVNamespace }).oauth

return await kv.get<OAuthSessionData>(key, 'json') ?? undefined
const value = await kv.get<OAuthSessionData>(key, 'json')

if (value === null || value.expiresAt < Date.now()) {
return
}

return value
} else {
if (!KV) {
KV = await Deno.openKv('oauth')
}

const result = await KV.get<OAuthSessionData>([key], {
consistency: 'strong',
consistency: 'eventual',
})

return result.value ?? undefined
if (result.value === null) {
return
}

if (result.value.expiresAt > Date.now()) {
return result.value
}

await KV.delete([key])
}
},

async has(c, key) {
if (c.runtime === 'cloudflare') {
const kv = (c.__app.env as { oauth: KVNamespace }).oauth

return await kv.get<OAuthSessionData>(key, 'json') !== null
const value = await kv.get<OAuthSessionData>(key, 'json')

if (value === null) {
return false
}

return value.expiresAt > Date.now()
} else {
if (!KV) {
KV = await Deno.openKv('oauth')
}

const result = await KV.get<OAuthSessionData>([key], {
consistency: 'strong',
consistency: 'eventual',
})

return result.value !== null
if (result.value === null) {
return false
}

if (result.value.expiresAt > Date.now()) {
return true
}

await KV.delete([key])

return false
}
},

Expand Down

0 comments on commit 1ca9bba

Please sign in to comment.