Next.js - Weather Search App
This is an example app demonstrating how to use Schematic in a Next.js app with Clerk authentication. This app uses schematic-react for usage tracking and entitlement management via feature flags, and uses schematic-components for an embedded customer portal experience. The Github repo is here and can be cloned to run locally.
Prerequisites
In order to use this example app, you’ll need:
- A Schematic account
- A Clerk account
In order to make full use of the capabilities of Schematic components, you’ll also need:
- A Stripe account
- Stripe customer IDs stored in private metadata on your Clerk companies
Getting Started
-
Set up your Schematic account; add features for “Weather Search”, “Humidity”, and “Wind Speed”, create some plans and entitlements for these features, connect your Stripe account, and connect your Clerk account.
-
Set up your
.env
file:
- In the Schematic app, create a component and store its component ID in your
.env
file:
- In the Schematic app, create a new API key and store both the publishable key and secret in your
.env
file:
- Store your Clerk secret and publishable keys in your .env file:
- Install dependencies:
- Run the development server:
- Open http://localhost:3000 with your browser to see the result.
How it Works
- First, we wrap our client-side code with the
<SchematicProvider>
provider:
- Then, we set a user context with the
identify
function from theuseSchematicEvents
hook; this is called in theClientWrapper
component, which wraps all client-side code.
- When we complete a weather search, we log that usage to Schematic using the
track
function from theuseSchematicEvents
hook:
- To enforce entitlements, we use the
useSchematicFlag
hook in various locations:
- To display our customer portal, we use the
SchematicEmbed
component:
- To provide a temporary access token for our embedded component, we implement a backend route to exchange a secret access token for a temporary, company-scoped access token: