Gatsby Authentication
Learn to install and initialize Clerk in a new Gatsby application.
Overview
Clerk is the easiest way to add authentication and user management to your Gatsby application. This guide will walk you through the necessary steps to install and use Clerk in a new Gatsby application. After following this guide, you should have a working Gatsby app complete with:
- Fully fledged sign in and sign up flows.
- Google Social Login.
- Secure email/password authentication.
- A prebuilt user profile page.
Before you start
You need to create a Clerk Application in your Clerk Dashboard.
Create a new Gatsby app
Start by creating a new Gatsby application - this is usually done using the npx gatsby new CLI:
npx gatsby new
Choose a directory to create the app and follow the terminal prompts to set up your app with your preferred options.
Installing the plugin
1# Install the necessary Clerk packages2npm install gatsby-plugin-clerk
Add your environment variables.
To use Clerk with Gatsby, you will need to use the publishable key, and secret key found in the Dashboard added to your .env.development
.env.development1
Update gatsby.config.js
Make sure you add the following snippet to be able to load environment variables.:
1import type { GatsbyConfig } from "gatsby"23// add dotenv loading4require("dotenv").config({5path: `.env.${process.env.NODE_ENV}`,6})7const config: GatsbyConfig = {8siteMetadata: {9title: `clerk-test`,10siteUrl: `https://www.yourdomain.tld`,11},12graphqlTypegen: true,1314// add gatsby plugin15plugins: [16{17resolve: `gatsby-plugin-clerk`18},19],20}2122export default config23
Using the plugin
Client-side
Client-side components are imported directly from gatsby-plugin-clerk
.
1import React from 'react'2import {3SignIn,4SignedIn,5SignedOut,6UserButton7} from 'gatsby-plugin-clerk'89export default function IndexPage() {10return (11<>12<SignedIn>13<UserButton />14</SignedIn>15<SignedOut>16<SignIn />17</SignedOut>18</>19)20}
Server-Side Rendering (SSR)
You can also use Gatsby SSR using withServerAuth
from 'gatsby-plugin-clerk/ssr'
.
1import * as React from 'react';2import { GetServerData } from 'gatsby';3import { withServerAuth } from 'gatsby-plugin-clerk/ssr';45export const getServerData: GetServerData<any> = withServerAuth(6async props => {7return { props: { data: '1', auth: props.auth } };8},9{ loadUser: true },10);1112function SSRPage({ serverData }: any) {13return (14<main>15<h1>SSR Page with Clerk</h1>16<pre>{JSON.stringify(serverData, null, 2)}</pre>17</main>18);19}2021export default SSRPage;
Server API routes
Importing 'gatsby-plugin-clerk/api'
gives access to all the exports coming from @clerk/sdk-node
.
1import { clerkClient, withAuth } from 'gatsby-plugin-clerk/api';23interface ContactBody {4message: string;5}67const handler = withAuth(async (req, res) => {8const users = await clerkClient.users.getUserList();9res.send({ title: `We have ${users.length} users`, message: req.body.message, auth: req.auth });10});1112export default handler;
Next steps
- Learn more about the Clerk Components and the Clerk Hosted Pages.
- Get support or at least say hi in our Discord channel