@@ -33,8 +33,8 @@ Clerk is the easiest way to add authentication and user management to your Gatsb
3333
3434### Prerequisites
3535
36- - Gatsby v4 +
37- - Node.js v16 +
36+ - Gatsby v5 +
37+ - Node.js v18 +
3838
3939### Installation
4040
@@ -47,32 +47,33 @@ npm install gatsby-plugin-clerk
4747Make sure the following environment variables are set in a ` .env ` file:
4848
4949``` sh
50- CLERK_API_KEY=[backend-api-key]
50+ GATSBY_CLERK_PUBLISHABLE_KEY=your_publishable_key
51+ CLERK_SECRET_KEY=your_secret_key
5152```
5253
5354You can get these from the [ API Keys] ( https://dashboard.clerk.dev/last-active?path=api-keys ) screen in your Clerk dashboard.
5455
55- To initialize Clerk with your Gatsby application, simply register the plugin in your ` gatsby-config.ts ` /` gatsby-config.js ` file:
56+ To initialize Clerk with your Gatsby application, simply register the plugin in your ` gatsby-config.ts ` /` gatsby-config.js ` file.
57+ Also, use ` dotenv ` to access environment variables.
5658
5759``` ts
5860// gatsby-config.ts
5961import type { GatsbyConfig } from ' gatsby' ;
6062
63+ require (' dotenv' ).config ({
64+ path: ` .env.${process .env .NODE_ENV } ` ,
65+ });
66+
6167const config: GatsbyConfig = {
6268 // ...the rest of your config object
63- plugins: [
64- {
65- resolve: ' gatsby-plugin-clerk' ,
66- options: {
67- frontendApi: [frontend - api - key ],
68- },
69- },
70- ],
69+ plugins: [' gatsby-plugin-clerk' ],
7170};
7271
7372export default config ;
7473```
7574
75+ ### Client-side
76+
7677After those changes are made, you can use Clerk components in your pages.
7778
7879For example, in ` src/pages/index.tsx ` :
@@ -97,25 +98,51 @@ const IndexPage = () => {
9798export default IndexPage ;
9899```
99100
100- Or even during SSR:
101+ ### Server-Side Rendering ( SSR)
101102
102- ``` ts
103+ Using ` withServerAuth ` from ` 'gatsby-plugin-clerk/ssr' ` . Example file ` /pages/ssr.tsx ` :
104+
105+ ``` tsx
106+ import * as React from ' react' ;
107+ import { GetServerData } from ' gatsby' ;
103108import { withServerAuth } from ' gatsby-plugin-clerk/ssr' ;
104109
105- export const getServerData = withServerAuth (async ({ auth }) => {
106- const { getToken, sessionId, userId } = auth ;
107- // fetch data using auth data or a custom JWT
108- const hasuraToken = await getToken ({ template: ' hasura ' });
109- const posts = db .fetchPosts (hasuraToken );
110- return { props: { posts } };
111- });
110+ export const getServerData: GetServerData <any > = withServerAuth (
111+ async props => {
112+ return { props: { data: ' 1' , auth: props .auth } };
113+ },
114+ { loadUser: true },
115+ );
112116
113- const PostsPage = ({ serverData }) => {
114- console .log (serverData );
115- return <div >... < / div > ;
116- };
117+ function SSRPage({ serverData }: any ) {
118+ return (
119+ <main >
120+ <h1 >SSR Page with Clerk</h1 >
121+ <pre >{ JSON .stringify (serverData , null , 2 )} </pre >
122+ </main >
123+ );
124+ }
125+
126+ export default SSRPage ;
127+ ```
128+
129+ ### Server API routes
130+
131+ Importing ` 'gatsby-plugin-clerk/api' ` gives acces to all the exports coming from ` @clerk/sdk-node ` . Example file ` /api/hello.ts ` :
132+
133+ ``` ts
134+ import { clerkClient , withAuth } from ' gatsby-plugin-clerk/api' ;
135+
136+ interface ContactBody {
137+ message: string ;
138+ }
139+
140+ const handler = withAuth (async (req , res ) => {
141+ const users = await clerkClient .users .getUserList ();
142+ res .send ({ title: ` We have ${users .length } users ` , auth: req .auth });
143+ });
117144
118- export default PostsPage ;
145+ export default handler ;
119146```
120147
121148_ For further details and examples, please refer to our [ Documentation] ( https://clerk.dev/docs/get-started/gatsby?utm_source=github&utm_medium=gatsby_plugin_clerk ) ._
0 commit comments