-
Notifications
You must be signed in to change notification settings - Fork 44
Add ConvexProviderWithAuthKit for WorkOS AuthKit integration #55
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
Closed
nicknisi
wants to merge
2
commits into
get-convex:main
from
nicknisi:nicknisi/workos-authkit-provider
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": "./api-extractor-base.json", | ||
"mainEntryPointFilePath": "<projectFolder>/dist/types/react-authkit/index.d.ts", | ||
"dtsRollup": { | ||
"untrimmedFilePath": "<projectFolder>/dist/types/react-authkit/react-authkit-internal.d.ts", | ||
"publicTrimmedFilePath": "<projectFolder>/dist/types/react-authkit/react-authkit.d.ts" | ||
}, | ||
/** | ||
* Enable the apiReport but use the same reportFolder and tempFolder to make it a no-op. | ||
* This way forgotten exports are a warning instead of an error. | ||
*/ | ||
"apiReport": { | ||
"enabled": true, | ||
"reportFileName": "react-authkit-tmp.api.md", | ||
"reportFolder": "temp", | ||
"reportTempFolder": "temp" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"main": "../dist/cjs/react-authkit/index.js", | ||
"module": "../dist/esm/react-authkit/index.js", | ||
"types": "../dist/cjs-types/react-authkit/index.d.ts" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @vitest-environment custom-vitest-environment.ts | ||
*/ | ||
import { test } from "vitest"; | ||
import React from "react"; | ||
import { ConvexProviderWithAuthKit } from "./ConvexProviderWithAuthKit.js"; | ||
import { ConvexReactClient } from "../react/index.js"; | ||
import { useAuth } from "@workos-inc/authkit-react"; | ||
|
||
test("Helpers are valid children", () => { | ||
const convex = new ConvexReactClient("https://localhost:3001"); | ||
|
||
const _ = ( | ||
<ConvexProviderWithAuthKit client={convex} useAuth={useAuth}> | ||
Hello world | ||
</ConvexProviderWithAuthKit> | ||
); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React from "react"; | ||
|
||
import { ReactNode, useCallback, useMemo } from "react"; | ||
import { AuthTokenFetcher } from "../browser/sync/client.js"; | ||
import { ConvexProviderWithAuth } from "../react/ConvexAuthState.js"; | ||
|
||
type IConvexReactClient = { | ||
setAuth(fetchToken: AuthTokenFetcher): void; | ||
clearAuth(): void; | ||
}; | ||
|
||
// Modified to match WorkOS's auth hook structure | ||
type UseAuth = () => { | ||
isLoading: boolean; | ||
user: any | null; | ||
getAccessToken: () => Promise<string | null>; | ||
}; | ||
|
||
/** | ||
* A wrapper React component which provides a {@link react.ConvexReactClient} | ||
* authenticated with WorkOS AuthKit. | ||
* | ||
* It must be wrapped by a configured `AuthKitProvider`, from | ||
* `@workos-inc/authkit-react`. | ||
* | ||
* @public | ||
*/ | ||
export function ConvexProviderWithAuthKit({ | ||
children, | ||
client, | ||
useAuth, | ||
}: { | ||
children: ReactNode; | ||
client: IConvexReactClient; | ||
useAuth: UseAuth; | ||
}) { | ||
const useAuthFromWorkOS = useUseAuthFromAuthKit(useAuth); | ||
return ( | ||
<ConvexProviderWithAuth client={client} useAuth={useAuthFromWorkOS}> | ||
{children} | ||
</ConvexProviderWithAuth> | ||
); | ||
} | ||
|
||
function useUseAuthFromAuthKit(useAuth: UseAuth) { | ||
return useMemo( | ||
() => | ||
function useAuthFromWorkOS() { | ||
const { isLoading, user, getAccessToken } = useAuth(); | ||
|
||
const fetchAccessToken = useCallback(async () => { | ||
try { | ||
const token = await getAccessToken(); | ||
return token; | ||
} catch { | ||
return null; | ||
} | ||
}, [getAccessToken]); | ||
|
||
return useMemo( | ||
() => ({ | ||
isLoading, | ||
isAuthenticated: !!user, | ||
fetchAccessToken, | ||
}), | ||
[isLoading, user, fetchAccessToken], | ||
); | ||
}, | ||
[useAuth], | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* React login component for use with WorkOS AuthKit | ||
* | ||
* @module | ||
*/ | ||
export { ConvexProviderWithAuthKit } from "./ConvexProviderWithAuthKit.js"; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@nicknisi since this fetchAccessToken can't actually force a refreshed token this is breaking the state machine, looking into whether we can relax this or we really want to expect a refreshed token
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 see that client.getAccessToken() has a forceRefresh, but agree that this is unnecessary if we can trust the AuthKit client to always have fresh-enough auth. Alternately we can build a new interface that allows WorkOS to tell us when a token has been refreshed.
The time the Convex client calls fetchToken with
forceRefreshToken: true
areIn case (3), if the token returned is not different than the previous token, the Convex client gives up on automated refreshing. This is the only behavior that needs to be changed, but in order to make other changes to the JWT instantly reactive (the user changes orgs, changes their, name, etc.).
Also I want to look into whether we're properly reactive to changes in the JWT, which are intended to be communicated by the identity of fetchToken changing (a new fetchToken is interpreted to mean it should be called again)
Just making a note, will get back to this later.