Skip to content

Commit 97b1c71

Browse files
authored
chore(astro): Update README.md (#3782)
1 parent 86c75e5 commit 97b1c71

File tree

2 files changed

+34
-209
lines changed

2 files changed

+34
-209
lines changed

.changeset/pretty-ways-doubt.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/astro/README.md

Lines changed: 32 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -96,244 +96,67 @@ PUBLIC_CLERK_SIGN_UP_URL=/sign-up # update this if sign up page exists on anothe
9696

9797
### Add integrations
9898

99-
- Add the `clerk` integration in your `astro.config.mjs` file.
100-
- (Optional) Install the `@astrojs/react` and add the `react` in your `astro.config.mjs` file. You only need to perform this action if you are planing to use react with your project or the React features that provided by `astro-clerk-auth`. [Instructions](https://docs.astro.build/en/guides/integrations-guide/react/)
101-
- Install the `@astrojs/node` package and the `node` adapter in your `astro.config.mjs` file. [Instructions](https://docs.astro.build/en/guides/server-side-rendering/)
102-
- Set `output` to `server`.
99+
Follow [the instructions](https://clerk.com/docs/quickstarts/astro) in our documentation.
103100

104101
Example configuration file
105102

106103
```js
107104
import { defineConfig } from 'astro/config';
108-
import react from '@astrojs/react';
109105
import node from '@astrojs/node';
110106
import clerk from '@clerk/astro';
111107

112108
export default defineConfig({
113-
integrations: [
114-
react(),
115-
clerk({
116-
afterSignInUrl: '/',
117-
afterSignUpUrl: '/',
118-
}),
119-
],
109+
integrations: [clerk()],
110+
adapter: node({ mode: 'standalone' }),
120111
output: 'server',
121-
adapter: node({
122-
mode: 'standalone',
123-
}),
124112
});
125113
```
126114

127115
### Add a middleware file
128116

129117
This step is required in order to use SSR or any control component. Create a `middleware.ts` file inside the `src/` directory.
130118

131-
**Simple use**
132-
133119
```ts
134120
import { clerkMiddleware } from '@clerk/astro/server';
135121

136122
export const onRequest = clerkMiddleware();
137123
```
138124

139-
**Supports chaining with `sequence`**
140-
141-
```ts
142-
const greeting = defineMiddleware(async (context, next) => {
143-
console.log('greeting request');
144-
console.log(context.locals.auth());
145-
const response = await next();
146-
console.log('greeting response');
147-
return response;
148-
});
149-
150-
export const onRequest = sequence(clerkMiddleware(), greeting);
151-
```
152-
153-
**Advanced use with handler**
154-
155-
```ts
156-
const isProtectedPage = createRouteMatcher(['/user(.*)', '/discover(.*)', /^\/organization/]);
157-
158-
export const onRequest = clerkMiddleware((auth, context, next) => {
159-
const requestURL = new URL(context.request.url);
160-
if (['/sign-in', '/', '/sign-up'].includes(requestURL.pathname)) {
161-
return next();
162-
}
163-
164-
if (isProtectedPage(context.request) && !auth().userId) {
165-
return auth().redirectToSignIn();
166-
}
167-
168-
if (!auth().orgId && requestURL.pathname !== '/discover' && requestURL.pathname === '/organization') {
169-
const searchParams = new URLSearchParams({
170-
redirectUrl: requestURL.href,
171-
});
172-
173-
const orgSelection = new URL(`/discover?${searchParams.toString()}`, context.request.url);
174-
175-
return context.redirect(orgSelection.href);
176-
}
177-
178-
return next();
179-
});
180-
```
181-
182-
### Use Clerk UI Components
183-
184-
Supported components
185-
186-
- [SignIn](https://clerk.com/docs/components/authentication/sign-in)
187-
- [SignUp](https://clerk.com/docs/components/authentication/sign-up)
188-
- [UseProfile](https://clerk.com/docs/components/user/user-profile)
189-
- [UserButton](https://clerk.com/docs/components/user/user-button)
190-
- [CreateOrganization](https://clerk.com/docs/components/organization/create-organization)
191-
- [OrganizationSwitcher](https://clerk.com/docs/components/organization/organization-switcher)
192-
- [OrganizationList](https://clerk.com/docs/components/organization/organization-list)
193-
- [OrganizationProfile](https://clerk.com/docs/components/organization/organization-profile)
194-
195-
All of the above can be used with React or Vanilla JS. The only difference is the import path.
196-
197-
```ts
198-
// Import UserProfile build with React (requires `@astro/react`)
199-
import { UserProfile } from '@clerk/astro/client/react';
200-
201-
// Import UserButton build with vanilla JS
202-
import { UserProfile } from '@clerk/astro/components/interactive';
203-
```
204-
205-
Pages that include a Clerk UI component need to be wrapped with `ClerkLayout`, as shown above.
206-
207-
### Use Clerk Control Components
208-
209-
Supported components
210-
211-
- [SignedIn](https://clerk.com/docs/components/control/signed-in)
212-
- [SignedOut](https://clerk.com/docs/components/control/signed-out)
213-
- [Protect](https://clerk.com/docs/components/protect)
214-
215-
All of the above can be used with React or only on server. The only difference is the import path.
216-
217-
```ts
218-
// Import Protect build with React (requires `@astro/react`)
219-
import { Protect } from '@clerk/astro/client/react';
220-
221-
// Import SignedIn build server side code
222-
import { SignedIn } from '@clerk/astro/components/control';
223-
```
224-
225-
### Protect your API Routes
226-
227-
In this example we are fetching the logged in user.
228-
229-
```ts
230-
import type { APIRoute } from 'astro';
231-
232-
const unautorized = () =>
233-
new Response(JSON.stringify({ error: 'unathorized access' }), {
234-
status: 401,
235-
});
236-
237-
export const GET: APIRoute = async ({ locals }) => {
238-
if (!locals.auth().userId) {
239-
return unautorized();
240-
}
241-
242-
return new Response(JSON.stringify(await locals.currentUser()), {
243-
status: 200,
244-
});
245-
};
246-
```
247-
248-
### Use Astro.locals
249-
250-
- Use `Astro.locals.auth()` to retrieve the [Authentication Object](https://clerk.com/docs/references/nextjs/authentication-object#authentication-object)
251-
252-
### Use Clerk react hooks
253-
254-
Example SignedIn React component that **supports SSR**
255-
256-
```tsx
257-
import type { PropsWithChildren } from 'react';
258-
import { useAuth } from '@clerk/astro/client/react';
259-
260-
export function SignedIn(props: PropsWithChildren) {
261-
const { userId } = useAuth();
262-
263-
if (!userId) {
264-
return null;
265-
}
266-
return props.children;
267-
}
268-
```
269-
270-
### Use a client store to build your custom logic
125+
### Use components inside .astro files
271126

272-
Warning: **SSR not supported**
273-
274-
```tsx
275-
import type { PropsWithChildren } from 'react';
276-
import { useStore } from '@nanostores/react';
277-
import { $authStore } from '@clerk/astro/client/stores';
278-
279-
export function SignedOut(props: PropsWithChildren) {
280-
const { userId } = useStore($authStore);
281-
282-
if (userId) {
283-
return null;
284-
}
285-
return props.children;
286-
}
287-
```
288-
289-
### Use Clerk react components inside your components
290-
291-
Example Header react component that uses Clerk components
292-
293-
```tsx
294-
import { SignedIn, SignedOut, UserButton } from '@clerk/astro/client/react';
127+
```astro
128+
---
129+
import { SignedIn, SignedOut, UserButton, SignInButton } from "@clerk/astro/components";
130+
---
295131
296-
export function Header() {
297-
return (
132+
<html lang="en">
133+
<head>
134+
<meta charset="utf-8" />
135+
<meta name="viewport" content="width=device-width, initial-scale=1" />
136+
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
137+
<meta name="generator" content={Astro.generator} />
138+
</head>
139+
<body>
298140
<header>
299-
<h1>My App</h1>
300-
<SignedIn>
301-
<UserButton />
302-
</SignedIn>
303-
<SignedOut>
304-
<a href='/sign-in'>Go to Sign in</a>
305-
</SignedOut>
141+
<h1>{title}</h1>
142+
<nav>
143+
<SignedOut>
144+
<SignInButton mode="modal" />
145+
</SignedOut>
146+
<SignedIn>
147+
<UserButton />
148+
</SignedIn>
149+
</nav>
306150
</header>
307-
);
308-
}
151+
<article>
152+
<slot />
153+
</article>
154+
</body>
155+
</html>
309156
```
310157

311-
### Use Clerk in Headless Mode
312-
313-
[Clerk Headless mode](https://clerk.com/docs/components/clerk-provider) (see `ClerkJSVariant` prop their docs) is a Clerk variant that is focused towards getting smaller bundle sizes. This variant does _not_ include React or any client side components for Clerk (e.g. their signin component). Because of that the bundle size is drastically smaller. On top of that it also lazy loads the JavaScript client side.
314-
315-
In order to use headless mode with this package, change your Astro configuration file to:
316-
317-
```diff
318-
import { defineConfig } from "astro/config";
319-
import react from "@astrojs/react";
320-
import node from "@astrojs/node";
321-
- import clerk from "@clerk/astro";
322-
+ import clerk from "@clerk/astro/hotload";
323-
324-
export default defineConfig({
325-
integrations: [
326-
react(),
327-
clerk({
328-
+ clerkJSVariant: "headless"
329-
}),
330-
],
331-
output: "server",
332-
adapter: node({
333-
mode: "standalone",
334-
}),
335-
});
336-
```
158+
_For further details and examples, please refer to
159+
our [Documentation](https://clerk.com/docs?utm_source=github&utm_medium=clerk_nextjs)._
337160

338161
## Support
339162

0 commit comments

Comments
 (0)