|
27 | 27 |
|
28 | 28 | ## Getting Started
|
29 | 29 |
|
30 |
| -[Clerk](https://clerk.com/?utm_source=github&utm_medium=clerk_chrome_extension) is the easiest way to add authentication and user management to your Chrome Extension. Add sign up, sign in, and profile management to your application in minutes. |
| 30 | +[Clerk](https://clerk.com/?utm_source=github&utm_medium=clerk_chrome_extension) is the easiest way to add authentication and user management to your Browser Extension. Add sign up, sign in, and profile management to your application in minutes. |
31 | 31 |
|
32 | 32 | ### Prerequisites
|
33 | 33 |
|
34 | 34 | - Node.js `>=18.17.0` or later
|
35 | 35 | - An existing Clerk application. [Create your account for free](https://dashboard.clerk.com/sign-up?utm_source=github&utm_medium=clerk_chrome_extension).
|
36 | 36 | - An existing React app (using [Vite](https://crxjs.dev/vite-plugin/) for example)
|
37 | 37 |
|
38 |
| -### Installation |
39 |
| - |
40 |
| -1. Add `@clerk/chrome-extension` to your project: |
41 |
| - |
42 |
| - ```shell |
43 |
| - npm install @clerk/chrome-extension |
44 |
| - ``` |
45 |
| - |
46 |
| -1. Retrieve the **Publishable key** from your [Clerk dashboard](https://dashboard.clerk.com/last-active?path=api-keys) and set it as an environment variable. For example, if you used Vite: |
47 |
| - |
48 |
| - ```sh |
49 |
| - VITE_CLERK_PUBLISHABLE_KEY=pk_test_xxx |
50 |
| - ``` |
51 |
| - |
52 |
| -1. Add `<ClerkProvider>` to your app and define the `routerPush` & `routerReplace` properties. For example, with using `react-router-dom`: |
53 |
| - |
54 |
| - ```tsx |
55 |
| - // App.tsx |
56 |
| - import { SignedIn, SignedOut, SignIn, SignUp, ClerkProvider } from '@clerk/chrome-extension'; |
57 |
| - import { useNavigate, Routes, Route, MemoryRouter } from 'react-router-dom'; |
58 |
| - |
59 |
| - function HelloUser() { |
60 |
| - return <p>Hello user</p>; |
61 |
| - } |
62 |
| - |
63 |
| - const publishableKey = process.env.VITE_CLERK_PUBLISHABLE_KEY || ''; |
64 |
| - |
65 |
| - function ClerkProviderWithRoutes() { |
66 |
| - const navigate = useNavigate(); |
67 |
| - |
68 |
| - return ( |
69 |
| - <ClerkProvider |
70 |
| - publishableKey={publishableKey} |
71 |
| - routerPush={to => navigate(to)} |
72 |
| - routerReplace={to => navigate(to, { replace: true })} |
73 |
| - > |
74 |
| - <Routes> |
75 |
| - <Route |
76 |
| - path='/sign-up/*' |
77 |
| - element={<SignUp signInUrl='/' />} |
78 |
| - /> |
79 |
| - <Route |
80 |
| - path='/' |
81 |
| - element={ |
82 |
| - <> |
83 |
| - <SignedIn> |
84 |
| - <HelloUser /> |
85 |
| - </SignedIn> |
86 |
| - <SignedOut> |
87 |
| - <SignIn |
88 |
| - afterSignInUrl='/' |
89 |
| - signUpUrl='/sign-up' |
90 |
| - /> |
91 |
| - </SignedOut> |
92 |
| - </> |
93 |
| - } |
94 |
| - /> |
95 |
| - </Routes> |
96 |
| - </ClerkProvider> |
97 |
| - ); |
98 |
| - } |
99 |
| - |
100 |
| - function App() { |
101 |
| - return ( |
102 |
| - <MemoryRouter> |
103 |
| - <ClerkProviderWithRoutes /> |
104 |
| - </MemoryRouter> |
105 |
| - ); |
106 |
| - } |
107 |
| - |
108 |
| - export default App; |
109 |
| - ``` |
110 |
| - |
111 |
| -## Usage |
112 |
| - |
113 |
| -Example repositories: |
| 38 | +### Feature Support |
114 | 39 |
|
115 |
| -- [Standalone](https://github.com/clerk/clerk-chrome-extension-starter/tree/main): The extension is using its own authentication |
116 |
| -- [WebSSO](https://github.com/clerk/clerk-chrome-extension-starter/tree/webapp_sso): The extensions shares authentication with a website in the same browser |
117 |
| - |
118 |
| -### WebSSO |
119 |
| - |
120 |
| -If you want to use **WebSSO** (extension shares authentication state with a website in same browser) you'll need to add the `syncSessionWithTab` prop to `<ClerkProvider>`. |
| 40 | +Please see the latest extension [authentication support matrix](https://clerk.com/docs/references/chrome-extension/overview?utm_source=github&utm_medium=clerk_chrome_extension) in the Clerk documentation for more information. |
121 | 41 |
|
122 |
| -#### Extension Manifest (`manifest.json`) |
| 42 | +### Usage |
123 | 43 |
|
124 |
| -You must enable the following permissions in your `manifest.json` file: |
| 44 | +1. **Installation:** `npm install @clerk/chrome-extension` |
| 45 | +2. **Set a consistent extension key**: A browser extension can be identified by its unique key, in a similar way to how a website can be identified by its domain. You will need to explicitly configure your extension's key or it will change often. If the key changes, it can cause the extension to fail. See the [Configure a Consistent Key](https://clerk.com/docs/references/chrome-extension/configure-consistent-crx-id?utm_source=github&utm_medium=clerk_chrome_extension) guide for more information. |
| 46 | +3. **Update Clerk Settings**: Once you've set up a consistent extension key, you'll need to configure your Clerk settings to allow the extension to communicate with your Clerk API. |
| 47 | + You can do this by adding the extension key to the list of allowed origins in your Clerk settings. Setting the `allowed_origins` is **required** for both **Development** and **Production** instances. |
125 | 48 |
|
126 |
| -```json |
127 |
| -{ |
128 |
| - "permissions": ["cookies", "storage"] |
129 |
| -} |
130 |
| -``` |
| 49 | + ```bash |
| 50 | + curl -X PATCH https://api.clerk.com/v1/instance \ |
| 51 | + -H "Content-type: application/json" \ |
| 52 | + -H "Authorization: Bearer <CLERK_SECRET_KEY>" \ |
| 53 | + -d '{"allowed_origins": ["chrome-extension://<YOUR_EXTENSION_KEY>"]}' |
| 54 | + ``` |
131 | 55 |
|
132 |
| -More info on the "cookies" permission: [Google Developer Cookies Reference](https://developer.chrome.com/docs/extensions/reference/cookies/). |
133 |
| -More info on the "storage" permission: [Google Developer Storage Reference](https://developer.chrome.com/docs/extensions/reference/storage/). |
| 56 | +4. **Set Environment Variables:** Retrieve the **Publishable key** from your [Clerk dashboard](https://dashboard.clerk.com/last-active?path=api-keys&utm_source=github&utm_medium=clerk_chrome_extension) and set it as an environment variable. |
134 | 57 |
|
135 |
| -#### Host Permissions |
| 58 | + ```sh |
| 59 | + # Vite |
| 60 | + VITE_CLERK_PUBLISHABLE_KEY=pk_test_xxx |
| 61 | + ``` |
136 | 62 |
|
137 |
| -You must enable the following host permissions in your `manifest.json` file: |
| 63 | + ```sh |
| 64 | + # Plasmo |
| 65 | + PLASMO_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_xxx |
| 66 | + ``` |
138 | 67 |
|
139 |
| -- **Development:** `"host_permissions": ["http://localhost"]` |
140 |
| - - If you're using a domain other than `localhost`, you'll want replace that entry with your domain: `http://<DOMAIN>` |
141 |
| -- **Production:** `"host_permissions": ["https://<YOUR_CLERK_FRONTEND_API_GOES_HERE>/"]` |
142 |
| - - Your Frontend API URL can be found in [Clerk Dashboard](https://dashboard.clerk.com/last-active?path=api-keys) under the **Show API URLs** option. |
| 68 | +5. **Update the extension manifest:** You'll need to update your extension manifest permissions to support Clerk. |
| 69 | + 1. [**Base configuration**:](/packages/chrome-extension/docs/manifest.md#base-configuration) Use this if you plan to only use Clerk in the context of the extention. |
| 70 | + 2. [**Session sync configuration**:](/packages/chrome-extension/docs/manifest.md#sync-host-configuration) Use this if you plan to share authentication with a website in the same browser. |
| 71 | +6. **Add Clerk to your app:** Though not required, we generally suggest using Plasmo for browser extension development. This will enforce common standards across your extension as well as allow for easier integration with other browsers in the future. |
143 | 72 |
|
144 |
| -For more info on host permissions visit [Google's developer `host_permissions` reference](https://developer.chrome.com/docs/extensions/mv3/declare_permissions/#host-permissions). |
| 73 | + 1. [**Via `ClerkProvider`:**](/packages/chrome-extension/docs/clerk-provider.md) This is the general approach to all extensions. From here you'll be able to support extension-only authentication as well as sharing authentication with a website in the same browser. |
| 74 | + 2. [**Via service workers**:](/packages/chrome-extension/docs/service-workers.md) If you also require the use of background service workers, this will allow you to access the Clerk client from the extension context. |
145 | 75 |
|
146 |
| -#### Clerk Settings |
| 76 | +## Example repositories |
147 | 77 |
|
148 |
| -Add your Chrome extension origin to your instance's `allowed_origins` using the [Backend API](https://clerk.com/docs/reference/backend-api): |
149 |
| - |
150 |
| -```bash |
151 |
| -curl -X PATCH https://api.clerk.com/v1/instance \ |
152 |
| - -H "Authorization: Bearer sk_secret_key" \ |
153 |
| - -H "Content-type: application/json" \ |
154 |
| - -d '{"allowed_origins": ["chrome-extension://extension_id_goes_here"]}' |
155 |
| -``` |
156 |
| - |
157 |
| -Setting the `allowed_origins` is **required** for both **Development** and **Production** instances. |
| 78 | +- [Standalone](https://github.com/clerk/clerk-chrome-extension-starter/tree/main): The extension is using its own authentication |
| 79 | +- [WebSSO](https://github.com/clerk/clerk-chrome-extension-starter/tree/webapp_sso): The extensions shares authentication with a website in the same browser |
158 | 80 |
|
159 | 81 | ## Support
|
160 | 82 |
|
|
0 commit comments