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(providers): Add Clerk OAuth Provider #11349

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/2_bug_provider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ body:
- "Beyond Identity"
- "Box"
- "Bungie"
- "Clerk"
- "ClickUp"
- "Cognito"
- "Coinbase"
Expand Down
2 changes: 2 additions & 0 deletions apps/dev/express/src/config/auth.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Apple from "@auth/express/providers/apple"
import Auth0 from "@auth/express/providers/auth0"
import AzureB2C from "@auth/express/providers/azure-ad-b2c"
import BoxyHQSAML from "@auth/express/providers/boxyhq-saml"
import Clerk from "@auth/express/providers/clerk"
import Cognito from "@auth/express/providers/cognito"
import Coinbase from "@auth/express/providers/coinbase"
import Discord from "@auth/express/providers/discord"
Expand Down Expand Up @@ -41,6 +42,7 @@ export const authConfig = {
clientSecret: "dummy",
issuer: process.env.AUTH_BOXYHQ_SAML_ISSUER,
}),
Clerk,
Cognito,
Coinbase,
Discord,
Expand Down
7 changes: 6 additions & 1 deletion apps/dev/nextjs/.env.local.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Rename file to .env.local (or .env) and populate values
# to be able to run the dev app

# You can use `openssl rand -hex 32` or
# You can use `openssl rand -hex 32` or
# https://generate-secret.vercel.app/32 to generate a secret.
# Note: Changing a secret may invalidate existing sessions
# and/or verification tokens.
Expand All @@ -24,6 +24,11 @@ AUTH_BEYOND_IDENTITY_CLIENT_ID=
AUTH_BEYOND_IDENTITY_CLIENT_SECRET=
AUTH_BEYOND_IDENTITY_ISSUER=

# When creating an OAuth2 Clerk application, set redirect url to "<DOMAIN>/auth/callback/clerk",
AUTH_CLERK_ID=
AUTH_CLERK_SECRET=
AUTH_CLERK_URL=https://<URL>.clerk.accounts.dev

AUTH_DESCOPE_ID=
AUTH_DESCOPE_SECRET=

Expand Down
1 change: 1 addition & 0 deletions apps/dev/nextjs/app/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function Client() {
</>
) : (
<>
<button onClick={() => signIn("clerk")}>Sign in Clerk</button>
<button onClick={() => signIn("github")}>Sign in GitHub</button>
<button onClick={() => signIn("credentials", {})}>
Sign in Credentials
Expand Down
8 changes: 7 additions & 1 deletion apps/dev/nextjs/auth.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NextAuthConfig } from "next-auth"
import Clerk from "next-auth/providers/clerk"
import Credentials from "next-auth/providers/credentials"
import GitHub from "next-auth/providers/github"
import Google from "next-auth/providers/google"
Expand Down Expand Up @@ -26,6 +27,11 @@ declare module "next-auth" {
export default {
debug: true,
providers: [
Clerk({
clientId: process.env.AUTH_CLERK_ID,
clientSecret: process.env.AUTH_CLERK_SECRET,
baseUrl: process.env.AUTH_CLERK_URL,
}),
Credentials({
credentials: { password: { label: "Password", type: "password" } },
authorize(c) {
Expand All @@ -39,7 +45,7 @@ export default {
}),
GitHub,
Google,
Keycloak,
// Keycloak,
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
Facebook,
Twitter,
LinkedIn,
Expand Down
2 changes: 2 additions & 0 deletions apps/examples/express/src/config/auth.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Apple from "@auth/express/providers/apple"
import Auth0 from "@auth/express/providers/auth0"
import AzureB2C from "@auth/express/providers/azure-ad-b2c"
import BoxyHQSAML from "@auth/express/providers/boxyhq-saml"
import Clerk from "@auth/express/providers/clerk"
import Cognito from "@auth/express/providers/cognito"
import Coinbase from "@auth/express/providers/coinbase"
import Discord from "@auth/express/providers/discord"
Expand Down Expand Up @@ -40,6 +41,7 @@ export const authConfig = {
clientSecret: "dummy",
issuer: process.env.AUTH_BOXYHQ_SAML_ISSUER,
}),
Clerk,
Cognito,
Coinbase,
Discord,
Expand Down
2 changes: 2 additions & 0 deletions apps/examples/nextjs-pages/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import NextAuth from "next-auth"
// import Box from "next-auth/providers/box"
// import BoxyHQSAML from "next-auth/providers/boxyhq-saml"
// import Bungie from "next-auth/providers/bungie"
// import Clerk from "next-auth/providers/clerk"
// import Cognito from "next-auth/providers/cognito"
// import Coinbase from "next-auth/providers/coinbase"
// import Discord from "next-auth/providers/discord"
Expand Down Expand Up @@ -78,6 +79,7 @@ export const config = {
// Box,
// BoxyHQSAML,
// Bungie,
// Clerk
// Cognito,
// Coinbase,
// Discord,
Expand Down
2 changes: 2 additions & 0 deletions apps/examples/nextjs/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Auth0 from "next-auth/providers/auth0"
import AzureB2C from "next-auth/providers/azure-ad-b2c"
import BankIDNorway from "next-auth/providers/bankid-no"
import BoxyHQSAML from "next-auth/providers/boxyhq-saml"
import Clerk from "next-auth/providers/clerk"
import Cognito from "next-auth/providers/cognito"
import Coinbase from "next-auth/providers/coinbase"
import Discord from "next-auth/providers/discord"
Expand Down Expand Up @@ -62,6 +63,7 @@ const config = {
clientSecret: "dummy",
issuer: process.env.AUTH_BOXYHQ_SAML_ISSUER,
}),
Clerk,
Cognito,
Coinbase,
Discord,
Expand Down
2 changes: 2 additions & 0 deletions apps/examples/sveltekit/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Apple from "@auth/sveltekit/providers/apple"
import Auth0 from "@auth/sveltekit/providers/auth0"
import AzureB2C from "@auth/sveltekit/providers/azure-ad-b2c"
import BoxyHQSAML from "@auth/sveltekit/providers/boxyhq-saml"
import Clerk from "@auth/sveltekit/providers/clerk"
import Cognito from "@auth/sveltekit/providers/cognito"
import Coinbase from "@auth/sveltekit/providers/coinbase"
import Discord from "@auth/sveltekit/providers/discord"
Expand Down Expand Up @@ -42,6 +43,7 @@ export const { handle, signIn, signOut } = SvelteKitAuth({
clientSecret: "dummy",
issuer: env.AUTH_BOXYHQ_SAML_ISSUER,
}),
Clerk,
Cognito,
Coinbase,
Discord,
Expand Down
2 changes: 2 additions & 0 deletions apps/proxy/api/[auth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Apple from "@auth/core/providers/apple"
import Auth0 from "@auth/core/providers/auth0"
import AzureB2C from "@auth/core/providers/azure-ad-b2c"
import BoxyHQSAML from "@auth/core/providers/boxyhq-saml"
import Clerk from "@auth/core/providers/clerk"
import Cognito from "@auth/core/providers/cognito"
import Coinbase from "@auth/core/providers/coinbase"
import Discord from "@auth/core/providers/discord"
Expand Down Expand Up @@ -40,6 +41,7 @@ const authConfig: AuthConfig = {
clientSecret: "dummy",
issuer: process.env.AUTH_BOXYHQ_SAML_ISSUER,
}),
Clerk,
Cognito,
Coinbase,
Discord,
Expand Down
1 change: 1 addition & 0 deletions docs/pages/data/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"box": "Box",
"boxyhq-saml": "BoxyHQ SAML",
"bungie": "Bungie",
"clerk": "Clerk",
"clickup": "ClickUp",
"cognito": "Cognito",
"coinbase": "Coinbase",
Expand Down
77 changes: 77 additions & 0 deletions docs/pages/getting-started/providers/clerk.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Code } from "@/components/Code"

<img align="right" src="/img/providers/clerk.svg" width="64" height="64" />

# Clerk Provider

## Resources

- [Clerk documentation](https://clerk.com/docs/advanced-usage/clerk-idp)

## Setup

### Callback URL

<Code>
<Code.Next>

```bash
https://example.com/api/auth/callback/clerk
```

</Code.Next>
<Code.Svelte>

```bash
https://example.com/auth/callback/clerk
```

</Code.Svelte>
</Code>

### Environment Variables

```
AUTH_CLERK_ID
AUTH_CLERK_SECRET
AUTH_CLERK_URL
```

### Configuration

<Code>
<Code.Next>

```ts filename="/auth.ts"
import NextAuth from "next-auth"
import Clerk from "next-auth/providers/clerk"

export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [Clerk],
})
```

</Code.Next>
<Code.Svelte>

```ts filename="/src/auth.ts"
import { SvelteKitAuth } from "@auth/sveltekit"
import Clerk from "@auth/sveltekit/providers/clerk"

export const { handle, signIn, signOut } = SvelteKitAuth({
providers: [Clerk],
})
```

</Code.Svelte>
<Code.Express>

```ts filename="/src/app.ts"
import { ExpressAuth } from "@auth/express"
import Clerk from "@auth/express/providers/clerk"

app.use("/auth/*", ExpressAuth({ providers: [Clerk] }))
```

</Code.Express>
</Code>
1 change: 1 addition & 0 deletions docs/public/img/providers/clerk.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
121 changes: 121 additions & 0 deletions packages/core/src/providers/clerk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center"}}>
* <span style={{fontSize: "1.35rem" }}>
* Built-in sign in with <b>Clerk</b> integration.
* </span>
* <a href="https://clerk.com" style={{backgroundColor: "black", padding: "12px", borderRadius: "100%" }}>
* <img style={{display: "block"}} src="https://authjs.dev/img/providers/clerk.svg" width="24"/>
* </a>
* </div>
*
* @module providers/clerk
*/
import type { OAuthConfig, OAuthUserConfig } from "./index.js"
Copy link
Member

Choose a reason for hiding this comment

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

Should be updated to OIDCConfig and OIDCUserConfig


/** @see [Get the authenticated user](https://clerk.com/docs/reference/frontend-api/tag/OAuth2-Identify-Provider#operation/getOAuthUserInfo). */
export interface ClerkProfile {
object: "string"
instance_id: "string"
email: "string"
email_verified: boolean
family_name: "string"
given_name: "string"
name: "string"
username: "string"
picture: "string"
user_id: "string"
public_metadata?: any
private_metadata?: any
unsafe_metadata?: any
}

/**
* Add Clerk login to your page.
*
* ### Setup
*
* #### Callback URL
* ```
* https://example.com/api/auth/callback/clerk
* ```
*
* #### Configuration
* ```ts
* import { Auth } from "@auth/core"
* import Clerk from "@auth/core/providers/clerk"
*
* const request = new Request(origin)
* const response = await Auth(request, {
* providers: [
* Clerk({
* clientId: CLERK_CLIENT_ID,
* clientSecret: CLERK_CLIENT_SECRET,
* baseUrl: CLERK_BASE_URL
* }),
Comment on lines +50 to +54
Copy link
Member

Choose a reason for hiding this comment

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

Should be

Suggested change
* Clerk({
* clientId: CLERK_CLIENT_ID,
* clientSecret: CLERK_CLIENT_SECRET,
* baseUrl: CLERK_BASE_URL
* }),
* Clerk,

This should be enough, if the user sets AUTH_CLERK_ID, AUTH_CLERK_SECRET and AUTH_CLERK_ISSUER

* ],
* })
* ```
*
* ### Resources
*
* - [Clerk - Use Clerk as an OAuth 2 Provider](https://clerk.com/docs/advanced-usage/clerk-idp)
Copy link
Member

Choose a reason for hiding this comment

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

*
* ### Notes
*
* By default, Auth.js assumes that the Clerk provider is
* based on the [OAuth 2](https://www.rfc-editor.org/rfc/rfc6749.html) specification.
*
* :::tip
*
* The Clerk provider comes with a [default configuration](https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/clerk.ts).
* To override the defaults for your use case, check out [customizing a built-in OAuth provider](https://authjs.dev/guides/configuring-oauth-providers).
*
* :::
*
* :::info **Disclaimer**
*
* If you think you found a bug in the default configuration, you can [open an issue](https://authjs.dev/new/provider-issue).
*
* Auth.js strictly adheres to the specification and it cannot take responsibility for any deviation from
* the spec by the provider. You can open an issue, but if the problem is non-compliance with the spec,
* we might not pursue a resolution. You can ask for more help in [Discussions](https://authjs.dev/new/github-discussions).
*
* :::
*/
export default function Clerk(
config: OAuthUserConfig<ClerkProfile> & {
baseUrl: string
Copy link
Member

Choose a reason for hiding this comment

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

No need for baseUrl or wellKnown, if the user passes issuer, we will construct the correct discovery URL.

Anything related to it should be dropped.

}
): OAuthConfig<ClerkProfile> {
const baseUrl = config.baseUrl

return {
id: "clerk",
name: "Clerk",
type: "oauth",
wellKnown: `${baseUrl}/.well-known/openid-configuration`,
authorization: {
url: `${baseUrl}/oauth/authorize`,
params: { scope: "email profile" },
},
token: `${baseUrl}/oauth/token`,
userinfo: {
url: `${baseUrl}/oauth/userinfo`,
async request({ tokens, provider }) {
const profile = await fetch(provider.userinfo?.url as URL, {
headers: {
Authorization: `Bearer ${tokens.access_token}`,
"User-Agent": "authjs",
},
}).then(async (res) => await res.json())

return profile
},
},
profile(profile) {
return profile
},
Comment on lines +95 to +117
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
type: "oauth",
wellKnown: `${baseUrl}/.well-known/openid-configuration`,
authorization: {
url: `${baseUrl}/oauth/authorize`,
params: { scope: "email profile" },
},
token: `${baseUrl}/oauth/token`,
userinfo: {
url: `${baseUrl}/oauth/userinfo`,
async request({ tokens, provider }) {
const profile = await fetch(provider.userinfo?.url as URL, {
headers: {
Authorization: `Bearer ${tokens.access_token}`,
"User-Agent": "authjs",
},
}).then(async (res) => await res.json())
return profile
},
},
profile(profile) {
return profile
},
type: "oidc",

When a provider is OIDC compliant, none of these need to be set

style: { bg: "#6C47FF", text: "#fff" },
options: config,
}
}
Loading