A wrapper theme to use Cognito (Amplify AWS) with your Gatsby website.
To use this theme in your Gatsby sites, follow these instructions:
-
Install the theme
npm install --save gatsby-theme-amplify-cognito
-
Set up a Cognito User Pool in AWS and create an App Client. Ensure that the 'App client secret' setting is set to '(no secret key)'
-
Add the theme to your
gatsby-config.js
:module.exports = { plugins: [{ resolve: `gatsby-theme-amplify-cognito`, options: { region: '', userPoolId: '', identityPoolId: '', userPoolWebClientId: '', // optional, array of paths that won't be authenticated doNotAuthenticate: ['/foo', '/bar/baz'], }, }] }
-
Populate the userPoolId with the Pool Id found under 'General Settings', and userPoolWebClientId with the 'App client id' found under 'App clients'
-
Populate the identityPoolId with the Id of the identity pool associated with your user pool. IF YOU WANT TO USE AWS SERVICE OBJECTS YOU WILL NEED TO SETUP AN IDENTITY POOL AND CONFIGURE THIS SETTING
-
(Optional) Configure any paths that don't require authentication by populating doNotAuthenticate with an array of paths
-
Each page is passed a prop of authState and authData which contain the details of the user session
-
Use the components to create your page:
import { SignIn, SignOut } from 'gatsby-theme-amplify-cognito'; const Homepage = ({authState, authData}) => { return <section> {(authState !== "signedIn") ? <SignIn authState={authState} /> : <> <h1>Hello {authData.username}</h1> <SignOut /> </> } </section> }
-
Start your site
gatsby develop