Skip to content

Commit

Permalink
feat(clerk auth): make getToken options globally configurable (#7947)
Browse files Browse the repository at this point in the history
* feat(clerk auth): make getToken configurable

* go with defaultGetTokenOptions

---------

Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com>
  • Loading branch information
jtoar and Tobbe authored Apr 1, 2023
1 parent 25edf6f commit 4ed587f
Showing 1 changed file with 21 additions and 9 deletions.
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

0 comments on commit 4ed587f

Please sign in to comment.