The Next.js Starter Kit includes the following
You can get started by running the following command:
This will run an installer that allows you to download, configure, and deploy, your project:
Give your project a name, and select the authentication method you’d like to use in your app. If you are trying to generate a proof of concept quickly, opting to use demo authentication may be a great option—you can still add other authentication providers later on.
If you would like to set up CI/CD with your application, we’ve made that process straightforward for you as well. When prompted, you can select "deploy to Vercel" to enable building and deploying the starter kit. The Vercel Integration will open in a new browser window for you to complete the process of adding your repository.
If by clicking "Create" you receive "An unexpected internal error occurred" you should validate within your code hosting platform of choice (GitHub, GitLab, Bitbucket) that the Vercel integration has permission to access your repository. In GitHub, permissions can be found can be found under Settings > Integrations > Vercel.
If you prefer to work locally, you can tell the installer you would not like to "deploy to Vercel." After declining the deployment option, the installer will prompt adding your Liveblocks Key to the application automatically. If you forwent deployment or indicated that you would like to get your Liveblocks secret key automatically, the Liveblocks integration page will open in a new browser tab. Once the integration page appears, you can sign up/sign in to Liveblocks, create a new project, and import your API key into the installer.
After finishing up, check the installer and follow the commands recommended to get you started.
The Liveblocks starter kit uses NextAuth.js for authentication, meaning many authentication providers can be configured with minimal code changes. A demo authentication system is used by default, but it’s easy to add real providers, such as GitHub, Auth0, and more.
Take a look at the guide for your chosen authentication method:
To use GitHub auth, make sure you selected "GitHub authentication" when running the installer (or you’ve set up the provider manually). This is how to set up your GitHub secret key and client id.
Liveblocks Starter Kit (dev)
). You’ll need a new
app for each environment, so it’s helpful to place "dev" in the name.http://localhost:3000
)./.env.local
file as GITHUB_CLIENT_SECRET
.env.local
file as GITHUB_CLIENT_ID
Almost there! .env.local
should now contain lines similar to this:
GitHub authentication is now complete! Next, add yourself as a user to test out your authentication.
To use Auth0 auth, make sure you selected "Auth0 authentication" when running the installer (or you’ve set up the provider manually). This is how to set up your Auth0 secret key and client information.
Liveblocks Starter Kit (dev)
). You’ll need a new
app for each environment, so it’s helpful to place "dev" in the name..env.local
as AUTH0_CLIENT_ID
..env.local
as
AUTH0_CLIENT_SECRET
..env.local
as AUTH0_ISSUER_BASE_URL
.http://localhost:3000/api/auth/callback/auth0
.http://localhost:3000
..env.local
should now contain these three lines, along with anything
previously there:
Auth0 authentication is now set up! Next, add yourself a user to test out your authentication.
For quickly testing out your app, the demo authentication method can be used.
This method uses a NextAuth
CredentialsProvider
to
simulate a real sign in system. To replace it with your own authentication
method, you can add any other NextAuth
Provider
to
/auth.config.ts
.
Read the next section to learn how to add a new user to your demo application.
We haven’t set up a database, so we’re temporarily using the
/data
folder instead. Before any user can sign in, they need to be added to
/data/users.ts
.
Navigate there and add your details, for example, if you’re signing in with
yourname@example.com
:
Note that this is replacing the sign-up process, so you must enter the email of any new user. If, for example, you’re using GitHub authentication, you can enter the email address of any valid GitHub account.
The Next.js Starter Kit is now ready to use! After setting up authentication, make sure to restart the dev server to see your authentication in action.
The starter kit has the following structure:
The important thing to note is that there are primitives, which are base level components such as buttons (think of these as the small building blocks of a page), components, which you can think of as multiplayer implementations (cursors, badges, etc), app, which renders document level experiences and a library (lib) of both server side and client side methods which contain the logic to create and modify documents.
/app/
/components/
/data/
/icons/
/layouts/
/lib/actions/
Server actions that can be used on both client and server, used for accessing and modifying documents.
/lib/database/
/lib/hooks/
/lib/utils/
/primitives/
/types/
/auth.ts
/auth.config.ts
/liveblocks.config.ts
/liveblocks.server.config.ts
This starter kit makes extensive use of the following programming pattern for fetching async resources:
Much of the starter kit’s power is in the
/lib/actions
directory. The functions in these files allow you to edit your documents easily
and return type-safe objects. For example in an API endpoint:
Because these functions are server actions, they can be leveraged on both client and server.
Functions that return data can be used with SWR hooks
that automatically update your data in components. For example,
getDocumentUsers
returns a list of users with access to the room:
Here’s a working example:
If you’d like to add a new property to Document
, it’s simple. First, edit the
Document
type in
/types/documents.ts
:
Then modify the return value in
/lib/utils/buildDocuments.ts
.
This is a function that converts a Liveblocks room into your custom document
format:
Next, run the following command to check for problems:
If no errors are returned, the document properties were successfully extended.
Similar to the way we extend the Document
type, we can also extend the User
and Session
type.
Adding a new property to User
/Session
is simple. First, edit the User
type
in
/types/data.ts
.
Then make sure to return this new property in
/lib/database/getUser.ts
.
The new property will now be available to use in your app:
Liveblocks presence is a way of displaying online presence between users,
helpful for live avatars, realtime cursors, etc., and it’s possible to attach a
properties to it for each user. To make a property accessible in presence (and
within the React hooks used in whiteboard), you must modify UserInfo
in
/liveblocks.config.ts
.
In this example
Pick creates
the UserInfo type based off of the User type and adds additional keys based on
the properties you provide. After this, modify
/lib/actions/authorizeLiveblocks.ts
.
First, we’ll give an anonymous user a property:
Next, we’ll get the signed-in user’s property:
And then pass this info to authorize
:
To make sure to check everything’s hooked up correctly:
Once that’s working, the new property can then be used in your app:
It’s possible to add multiple authentication providers to the starter kit using
NextAuth Providers. Open
/auth.config.ts
and place your providers in the object:
It’s not only possible with GitHub and Auth0, any NextAuth provider will work, such as Google, X, Reddit, or more. You can find more information about getting the necessary secrets on the NextAuth documentation, or on the provider’s website.
Note that if you’re using CredentialsProvider
(for example, as used in the
demo authentication), CredentialsProvider
must be removed before any other
authentication methods will appear.
The starter kit comes with both a dark mode and light mode. By default, the user
sees the theme that corresponds to their system setting, but it’s easy to switch
your whole app to just dark or light mode by modifying
styles/globals.css
.
@media (prefers-color-scheme: dark)
media query.@media (prefers-color-scheme: dark)
media query.To add a database you need to modify the following async functions to return the correct properties:
You can then remove the /data
folder. Everything else should work as expected.
We use cookies to collect data to improve your experience on our site. Read our Privacy Policy to learn more.