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

Conversation

jtoar
Copy link
Contributor

@jtoar jtoar commented Mar 28, 2023

Clerk users need to be able to configure how the auth provider calls getToken so that it always uses certain options. As far as I could tell this isn't possible right now. This PR suggests two different ways of doing it. Maybe we'll go with just one, or both, or neither, but it's important to at least get an RC out so that users can also tell us which solution they like.

The idea in this PR is that users could either provide a wrapper for getToken, or pass default options:

// web/src/auth.tsx

// ...

import { createAuth } from '@redwoodjs/auth-clerk-web'

export const { AuthProvider: ClerkRwAuthProvider, useAuth } = createAuth({
  // The user can provide a wrapper function (has to return a function)
  getToken: (cb) => (options) => cb({ ...options, option: value }),
  // Or they can provide an options object
  defaultGetTokenOptions: {
    option: value
  }
})

// ...

@thedavidprice thedavidprice added this to the next-release-patch milestone Mar 28, 2023
@thedavidprice thedavidprice self-assigned this Mar 28, 2023
@jtoar jtoar added the release:feature This PR introduces a new feature label Mar 28, 2023
@Tobbe
Copy link
Member

Tobbe commented Mar 31, 2023

Great work on this @jtoar!

A third option here would be to have the users give the full getToken implementation, to make it closer to how useCurrentUser and useHasRole works.

Whatever we choose, I think we should only choose one.

If we go with what's currently called getToken, I think we should rename it getTokenWrapper, because that's more descriptive of what it actually does. And use internalGetToken instead of just cb in docs/comments when showing example usage.

Actually, having written all the above, maybe we should just name it getToken, and not frame it as a wrapper, but frame it as an option to provide a totally custom implementation. But also pass in the default internal getToken implementation so that people can use that in their own custom implementation if they want, so we give them the option to treat it as just a wrapper if they want to. And maybe we should think about doing the same for useCurrentUser and useHasRole too.

But the easier option, both for us and for our users right now is of course to make it possible to pass in default options. So maybe we should go with that for now, and switch over to the other, more powerful, alternative when someone actually needs it.

@jtoar
Copy link
Contributor Author

jtoar commented Apr 1, 2023

Thanks @Tobbe, and sounds good, I've reduced the PR to just the defaultGetTokenOptions to keep things simple for now like you suggested

@jtoar jtoar enabled auto-merge (squash) April 1, 2023 14:51
@jtoar jtoar merged commit 4ed587f into main Apr 1, 2023
@jtoar jtoar deleted the ds-make-getToken-options-globally-configurable branch April 1, 2023 16:11
jtoar added a commit that referenced this pull request Apr 1, 2023
* feat(clerk auth): make getToken configurable

* go with defaultGetTokenOptions

---------

Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com>
@jtoar jtoar modified the milestones: next-release-patch, v4.4.3 Apr 2, 2023
jtoar added a commit that referenced this pull request Apr 2, 2023
* feat(clerk auth): make getToken configurable

* go with defaultGetTokenOptions

---------

Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com>
dac09 added a commit to dac09/redwood that referenced this pull request Apr 3, 2023
* 'main' of github.com:redwoodjs/redwood: (66 commits)
  update template README
  bumps API build target 16.20; fixtures updated (redwoodjs#7995)
  remove serverless deploy option (redwoodjs#7993)
  chore(deps): update dependency @clerk/types to v3.33.0 (redwoodjs#7991)
  feat(clerk auth): make `getToken` options globally configurable (redwoodjs#7947)
  chore(deps): update dependency firebase to v9.19.1 (redwoodjs#7986)
  fix(deps): update dependency webpack-dev-server to v4.13.2 (redwoodjs#7989)
  fix(deps): update dependency @clerk/clerk-sdk-node to v4.8.1 (redwoodjs#7988)
  fix upgrade.js error message (redwoodjs#7992)
  fix(clerk auth): set auth state to loading while reauthenticating (redwoodjs#7852)
  chore(deps): update dependency @supabase/supabase-js to v2.13.1 (redwoodjs#7981)
  chore(deps): update dependency @clerk/clerk-react to v4.14.1 (redwoodjs#7987)
  chore: unpublish packages script (redwoodjs#7990)
  chore(deps): update dependency nx to v15.9.2 (redwoodjs#7983)
  chore(deps): update dependency @nrwl/nx-cloud to v15.3.5 (redwoodjs#7985)
  fix(deps): update dependency @apollo/client to v3.7.11 (redwoodjs#7984)
  chore(deps): update dependency @tsd/typescript to v5.0.3 (redwoodjs#7978)
  chore(deps): update dependency typescript to v5.0.3 (redwoodjs#7972)
  chore(deps): update dependency @nrwl/nx-cloud to v15.3.4 (redwoodjs#7977)
  chore(deps): update dependency nx to v15.9.1 (redwoodjs#7976)
  ...
jaiakt pushed a commit to jaiakt/redwood that referenced this pull request Apr 5, 2023
…oodjs#7947)

* feat(clerk auth): make getToken configurable

* go with defaultGetTokenOptions

---------

Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature This PR introduces a new feature
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants