-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add networkless path for isAuthenticated #183
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -22,6 +22,7 @@ import { | |||||
setAuthCookiesInMiddleware, | ||||||
} from "./utils.js"; | ||||||
import { IsAuthenticatedQuery } from "../../server/implementation/index.js"; | ||||||
import { jwtVerify, createLocalJWKSet } from "jose" | ||||||
|
||||||
/** | ||||||
* Wrap your app with this provider in your root `layout.tsx`. | ||||||
|
@@ -302,6 +303,21 @@ async function isAuthenticated(token: string | null): Promise<boolean> { | |||||
if (!token) { | ||||||
return false; | ||||||
} | ||||||
|
||||||
// First, try using the JWKS from the environment variable to do token | ||||||
// verification locally (networkless mode). | ||||||
try { | ||||||
const envJwks = process.env.CONVEX_AUTH_JWKS | ||||||
if (envJwks) { | ||||||
const jwkSet = createLocalJWKSet(JSON.parse(envJwks)) | ||||||
const verifiedToken = await jwtVerify(token, jwkSet); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we put this + the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I forget whether we should be checking the expiration time of this token here? I believe previously this would return |
||||||
return !!verifiedToken.payload.sub; | ||||||
} | ||||||
} catch (error: any) { | ||||||
console.error("Error verifying token", error); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(we could also say something like "make sure this matches the value set in your Convex |
||||||
} | ||||||
|
||||||
// Fallback to asking the server to do verification. | ||||||
try { | ||||||
return await fetchQuery( | ||||||
"auth:isAuthenticated" as any as IsAuthenticatedQuery, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to ask if this is possible to configure via an argument in the provider or middleware, but I think the answer is "no" since these get wrapped in things that are imported directly.