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

Update Clerk auth provider logic #4443

Merged
merged 8 commits into from Feb 16, 2022
Merged

Conversation

devchampian
Copy link
Contributor

This PR includes 3 updates across 3 files:

  1. auth/providers/clerk.js - Simplified Clerk integration using the withClerk HOC. Explicit client and render props are no longer necessary.
  2. auth/templates/clerk.auth.js.template - Added Clerk auth template. Previously the base template was being used. The standard template was updated in the tutorial repo (includes the use of logger and underscore prefix on unused variables) so that same one was copied over for Clerk.
  3. auth/auth.js - The <AuthProvider /> in the Redwood tutorial was recently updated to remove the client prop with the introduction of dbAuth. This causes the yarn rw setup auth <provider> --force script to fail in replacing the auth provider. This is because the regex is looking for a client prop which no longer exists. The updated regex pattern works regardless whether the client prop is set or not.

The first two changes are specific to the Clerk integration. Only the third change affects the core auth setup logic.

@thedavidprice
Copy link
Contributor

thedavidprice commented Feb 11, 2022

Thank you for this @devchampian I'm looping in @dthyresson to help get this to the finish line (DT, please pass along if/as needed).

Question on my end:

  1. Is the Redwood Clerk Auth currently working as is? (I haven't tested in awhile.)
  2. For existing projects using Clerk, what instructions do we need to include in the Release Notes for upgrading to these templates?
  3. How to test/confirm the code in this PR is working: steps to use locally, external example project, ... ?
  4. Are the Redwood Clerk Docs in need of any related TLC?

@devchampian
Copy link
Contributor Author

  1. Is the Redwood Clerk Auth currently working as is? (I haven't tested in awhile.)

Yes, it is. The only exception is the small fix included in this PR around the CLI auth setup script replacing the Redwood <AuthProvider> with <AuthProvider type="clerk">

  1. For existing projects using Clerk, what instructions do we need to include in the Release Notes for upgrading to these templates?

The previous implementation is still supported by Clerk. The change to use the withClerk HOC was to remove the need for the <ClerkLoaded> component. The other changes were to remove unnecessary code. The auth template is a copy of the standard one so there is nothing Clerk-specific in there.

  1. How to test/confirm the code in this PR is working: steps to use locally, external example project, ... ?

So I did confirm it was working locally by following your contributing documentation on testing the CLI. If you pull down this branch, run yarn build and then yarn dev setup auth clerk --force into a Redwood project directory.

Caveat: I wasn't able to get the --cwd flag to work correctly so I did need to set the RWJS_CWD environment variable explicitly as well as add the cwd property to the execa('yarn', []) calls in packages/cli/src/commands/setup/auth/auth.js to point to a separate Redwood project directory.

  1. Are the Redwood Clerk Docs in need of any related TLC?

The only change needed in the docs would be to update web/src/App.js to match the version in this PR.

We are likely going to be making a more substantial change to the Clerk integration with our Auth v2 update in the future. I think at that point, we can also revisit the docs to make sure everything is up-to-date with all the latest goodness.

Thank you @thedavidprice for taking a look at this!

@thedavidprice thedavidprice added the release:feature This PR introduces a new feature label Feb 11, 2022
@devchampian
Copy link
Contributor Author

I also just posted this tutorial based on the official Redwood one, but adding Clerk auth. The corresponding codebase is here if you want to validate the changes with a sample application.

@dthyresson
Copy link
Contributor

I also just posted this tutorial based on the official Redwood one, but adding Clerk auth. The corresponding codebase is here if you want to validate the changes with a sample application.

This is excellent! Reading now. Thank you!

@thedavidprice
Copy link
Contributor

thedavidprice commented Feb 11, 2022

Thank you for answering my questions @devchampian

Yes, it is. The only exception is the small fix included in this PR around the CLI auth setup script replacing the Redwood with

^^ this feels like the most important callout to make in the Release Notes

@dthyresson once reviewed and approved, can you help me stay on top of ToDos related to copy and docs?

@dthyresson
Copy link
Contributor

Testing in GitPod now.

Todo:

The only change needed in the docs would be to update web/src/App.js to match the version in this PR.

@dthyresson
Copy link
Contributor

Nice!

image

@dthyresson
Copy link
Contributor

@devchampian Any chance when all integrated RedwoodJS could get a shout out here, too?
image

@dthyresson
Copy link
Contributor

Working in GitPod with test project.

image

image

For the docs - having a note about approving cookies for dev may be nice so that it isn'y an unexpected decisions.

But looks great!

@dthyresson
Copy link
Contributor

Also, I wish (and maybe will add in future) that setup created a component for the Clerk UserButton to make sign up even easier:

<li className={isAuthenticated ? 'ml-2' : null}>
              {isAuthenticated ? (
                <UserButton afterSignOutAll={window.location.href} />
              ) : (
                <SignInButton mode="modal">
                  <button className="py-2 px-4 hover:bg-blue-600 transition duration-100 rounded">
                    Log in
                  </button>
                </SignInButton>
              )}
            </li>
          </ul>
          {isAuthenticated && (
            <div className="text-right text-xs text-blue-300">
              {currentUser?.emailAddresses[0]?.emailAddress}
            </div>
          )}

And setup to somehow inject ClerkAuthProvider:

const App = () => (
  <FatalErrorBoundary page={FatalErrorPage}>
    <RedwoodProvider titleTemplate="%PageTitle | %AppTitle">
      <ClerkAuthProvider>
        <AuthProvider type="clerk">
          <RedwoodApolloProvider>
            <Routes />
          </RedwoodApolloProvider>
        </AuthProvider>
        </ClerkAuthProvider>
    </RedwoodProvider>
  </FatalErrorBoundary>
)

within RedwoodProvider.

That would make setup rather slick.

dthyresson
dthyresson previously approved these changes Feb 16, 2022
Copy link
Contributor

@dthyresson dthyresson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see some TS squiggles

image

But it appears to work fine. I'll dig a little into it but otherwise approved!

@devchampian
Copy link
Contributor Author

@dthyresson Awesome, thank you! And absolutely we can add a RedwoodJS shoutout. 😄

This was just a minor update, but as previously discussed, we would like to upgrade the integration to our Auth v2 so I think at that time we can also do a docs refresh.

I know Redwood has authentication built-in with dbAuth but it's great that it offers the flexibility to swap different providers for those folks who don't want to manage it themselves.

Completely agree with you about the yarn rw auth setup clerk command. Wish it could fill in more of the boilerplate. Is that something others have asked for?

@dthyresson
Copy link
Contributor

dthyresson commented Feb 16, 2022

For types, it might be this:

export declare const AuthContext: React.Context<AuthContextInterface>;
declare type AuthProviderProps = {
    client: SupportedAuthClients;
    type: Omit<SupportedAuthTypes, 'dbAuth'>;
    skipFetchCurrentUser?: boolean;
} | {
    client?: never;
    type: 'dbAuth';
    skipFetchCurrentUser?: boolean;
};

Where type needs to include clerk, too?

Edit: No, sorry. That's already in

export declare type SupportedAuthClients = Auth0 | AzureActiveDirectory | DbAuth | GoTrue | NetlifyIdentity | MagicLink | FirebaseClient | Supabase | Clerk | Ethereum | Nhost | SuperTokens | Custom;
export declare type SupportedAuthTypes = keyof typeof typesToClients;

Ok will have to look more closely.

@dthyresson
Copy link
Contributor

Wish it could fill in more of the boilerplate. Is that something others have asked for?

Not yet, but I know in v2 Redwood would like to make the setups more sophisticated and customizable, so for v1 I think the guides and tutorials work (but improve soon).

Honestly, I got the test project up and running in 2-3 minutes really.

@dthyresson
Copy link
Contributor

dthyresson commented Feb 16, 2022

we would like to upgrade the integration to our Auth v2 so I think at that time we can also do a docs refresh.

Do you think that would be possible in the next 3-4 weeks?

Edit: See #4459 (comment) for an update.

@dthyresson dthyresson added release:chore This PR is a chore (means nothing for users) and removed release:feature This PR introduces a new feature labels Feb 16, 2022
@dthyresson
Copy link
Contributor

Note adding

declare type AuthProviderProps = {
    client: SupportedAuthClients;
    type: Omit<SupportedAuthTypes, 'dbAuth'>;
    skipFetchCurrentUser?: boolean;
} | {
    client?: never;
    type: 'dbAuth' | 'clerk'; <<<----
    skipFetchCurrentUser?: boolean;
}

'clerk' to the type here appears to resolve one issue.

image

@dthyresson dthyresson dismissed their stale review February 16, 2022 16:03

Dismissing review as I'd like to update the AuthProvider types so no TS warnings.

@dthyresson dthyresson added release:fix This PR is a fix and removed release:chore This PR is a chore (means nothing for users) labels Feb 16, 2022
@dthyresson
Copy link
Contributor

I think that casting as React.ReactElement resolves the final TS warning.

const ClerkAuthConsumer = withClerk(({ children, clerk }) => {
  return React.cloneElement(children as React.ReactElement<any>, { client: clerk })
})

@dthyresson
Copy link
Contributor

Looks good.

image

Merging. Thanks all!

@dthyresson dthyresson merged commit 905ca4b into redwoodjs:main Feb 16, 2022
@jtoar jtoar added this to the next-release milestone Feb 16, 2022
@thedavidprice
Copy link
Contributor

Thanks, all!

@dthyresson @devchampian Could either of you help to make sure the Redwood Auth Doc is updated to match this PR? See:

@devchampian
Copy link
Contributor Author

image

Just one comment that it should be <ClerkAuthProvider> not <ClerkProvider> wrapped around the Redwood <AuthProvider> in this code snippet.

@thedavidprice
Copy link
Contributor

@dthyresson
Copy link
Contributor

dthyresson commented Feb 16, 2022

image

Just one comment that it should be <ClerkAuthProvider> not <ClerkProvider> wrapped around the Redwood <AuthProvider> in this code snippet.

Oops. that was me just testing for types in my last GitPod deploy. I never launched the app. But I had it correct before :)

@devchampian
Copy link
Contributor Author

@thedavidprice Actually for the context of wherever @dthyresson took that screenshot. But yes, the docs can be updated there as well.

@thedavidprice
Copy link
Contributor

@dthyresson Since I don't have context, could you help here with the final two things:

  1. update the Doc to match
  2. is there codemod or fix instructions I should include in the Release Notes? Assuming "yes".

@dthyresson
Copy link
Contributor

dthyresson commented Feb 17, 2022

@dthyresson Since I don't have context, could you help here with the final two things:

  1. update the Doc to match
  2. is there codemod or fix instructions I should include in the Release Notes? Assuming "yes".

@thedavidprice

Except for TS, then need a slight change to:

const ClerkAuthConsumer = withClerk(({ children, clerk }) => {
  return React.cloneElement(children as React.ReactElement<any>, { client: clerk })
})

@dthyresson
Copy link
Contributor

Just updated the RW Auth Playground to have Clerk as well.

image

It's running a canary build of 0.46. Once 0.46.1 is released, will update and deploy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix This PR is a fix
Projects
No open projects
Status: Archived
Development

Successfully merging this pull request may close these issues.

None yet

4 participants