Skip to content
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

feat(clerk auth): make getToken options globally configurable #7947

Merged
merged 5 commits into from
Apr 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions packages/auth-providers/clerk/web/src/clerk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ import { CurrentUser, createAuthentication } from '@redwoodjs/auth'

type Clerk = ClerkClient | undefined | null

export function createAuth(customProviderHooks?: {
useCurrentUser?: () => Promise<CurrentUser>
useHasRole?: (
currentUser: CurrentUser | null
) => (rolesToCheck: string | string[]) => boolean
}) {
const authImplementation = createAuthImplementation()
type AuthImplementationOptions = {
defaultGetTokenOptions?: GetTokenOptions
}

export function createAuth(
customProviderHooks?: {
useCurrentUser?: () => Promise<CurrentUser>
useHasRole?: (
currentUser: CurrentUser | null
) => (rolesToCheck: string | string[]) => boolean
},
authImplementationOptions?: AuthImplementationOptions
) {
const authImplementation = createAuthImplementation(authImplementationOptions)

return createAuthentication(authImplementation, customProviderHooks)
}

function createAuthImplementation() {
function createAuthImplementation({
defaultGetTokenOptions = {},
}: AuthImplementationOptions = {}) {
return {
type: 'clerk',
// Using a getter here to make sure we're always returning a fresh value
Expand Down Expand Up @@ -53,7 +62,10 @@ function createAuthImplementation() {
let token

try {
token = await clerk?.session?.getToken(options)
token = await clerk?.session?.getToken({
...defaultGetTokenOptions,
...options,
})
} catch {
token = null
}
Expand Down