Getting Started
Getting Started
Next.js Quickstart
Next.js Quickstart
Before you can start integrating Ollio into your application, you need to create a Ollio account and set up a new application in the Ollio Dashboard. This guide will walk you through those steps.
Before you can start integrating Ollio into your application, you need to create a Ollio account and set up a new application in the Ollio Dashboard. This guide will walk you through those steps.
Note
If you're migrating from another platform, see the migration guides to learn how to move your data to Ollio.
Note
If you're migrating from another platform, see the migration guides to learn how to move your data to Ollio.
1
Install @Ollio/nextjs
1
Install @Ollio/nextjs
Ollio's Next.js SDK gives you access to prebuilt components, React hooks, and helpers to make user authentication easier.
Run the following command to install the SDK:
Ollio's Next.js SDK gives you access to prebuilt components, React hooks, and helpers to make user authentication easier.
Run the following command to install the SDK:
npm
yarn
pnpm
terminal
npm install @Ollio/nextjs
npm
yarn
pnpm
terminal
npm install @Ollio/nextjs
2
Set your Ollio API keys
2
Set your Ollio API keys
.env.local
NEXT_PUBLIC_OLLIO_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY OLLIO_SECRET_KEY=YOUR_SECRET_KEY
.env.local
NEXT_PUBLIC_OLLIO_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY OLLIO_SECRET_KEY=YOUR_SECRET_KEY
3
Adding ollioMiddleware() to Your Application
3
Adding ollioMiddleware() to Your Application
ollioMiddleware()
provides seamless access to user authentication states throughout your application, regardless of the route or page. Additionally, it enables you to secure specific routes from unauthenticated users. To integrate ollioMiddleware()
into your app, follow these steps:
Create a
middleware.ts
file:If your app uses a
/src
directory, placemiddleware.ts
in the/src
directory.Otherwise, create
middleware.ts
in the root directory, alongside your.env.local
file.
Export the
ollioMiddleware()
helper in yourmiddleware.ts
file.
ollioMiddleware()
provides seamless access to user authentication states throughout your application, regardless of the route or page. Additionally, it enables you to secure specific routes from unauthenticated users. To integrate ollioMiddleware()
into your app, follow these steps:
Create a
middleware.ts
file:If your app uses a
/src
directory, placemiddleware.ts
in the/src
directory.Otherwise, create
middleware.ts
in the root directory, alongside your.env.local
file.
Export the
ollioMiddleware()
helper in yourmiddleware.ts
file.
middleware.ts
import { ollioMiddleware } from '@ollio/nextjs/server' export default ollioMiddleware() export const config = { matcher: [ // Exclude Next.js internals and static files, unless referenced in search params '/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)', // Ensure it runs for API routes '/(api|trpc)(.*)', ], }
middleware.ts
import { ollioMiddleware } from '@ollio/nextjs/server' export default ollioMiddleware() export const config = { matcher: [ // Exclude Next.js internals and static files, unless referenced in search params '/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)', // Ensure it runs for API routes '/(api|trpc)(.*)', ], }
By default,
ollioMiddleware()
does not protect any routes. All routes are accessible to the public, and you need to explicitly enable protection for specific routes. Refer to theollioMiddleware()
documentation for details on requiring authentication for specific routes.
By default,
ollioMiddleware()
does not protect any routes. All routes are accessible to the public, and you need to explicitly enable protection for specific routes. Refer to theollioMiddleware()
documentation for details on requiring authentication for specific routes.
4
Add <OllioProvider> and Ollio Components to Your App
4
Add <OllioProvider> and Ollio Components to Your App
The <OllioProvider>
component supplies session and user context to Ollio's hooks and components. It is recommended to wrap your entire application at the entry point with <OllioProvider>
to make authentication functionality globally available. Refer to the documentation for additional configuration options.
You can control the visibility of content for signed-in and signed-out users using Ollio's prebuilt control components. Create a header with the following components:
<SignedIn>
: Displays its children only when the user is signed in.<SignedOut>
: Displays its children only when the user is signed out.<UserButton />
: Displays the avatar of the signed-in user. Clicking it opens a dropdown menu for account management options.<SignInButton />
: An unstyled component that links to the sign-in page. In this example, since no props or environment variables are specified for the sign-in URL, the button redirects to the Account Portal sign-in page.
Choose your preferred routing setup to integrate this across your app:
The <OllioProvider>
component supplies session and user context to Ollio's hooks and components. It is recommended to wrap your entire application at the entry point with <OllioProvider>
to make authentication functionality globally available. Refer to the documentation for additional configuration options.
You can control the visibility of content for signed-in and signed-out users using Ollio's prebuilt control components. Create a header with the following components:
<SignedIn>
: Displays its children only when the user is signed in.<SignedOut>
: Displays its children only when the user is signed out.<UserButton />
: Displays the avatar of the signed-in user. Clicking it opens a dropdown menu for account management options.<SignInButton />
: An unstyled component that links to the sign-in page. In this example, since no props or environment variables are specified for the sign-in URL, the button redirects to the Account Portal sign-in page.
Choose your preferred routing setup to integrate this across your app:
App router
Page router
app/layout.tsx
import { OllioProvider, SignInButton, SignedIn, SignedOut, UserButton } from '@ollio/nextjs' import './globals.css' export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <OllioProvider> <html lang="en"> <body> <header> <SignedOut> <SignInButton /> </SignedOut> <SignedIn> <UserButton /> </SignedIn> </header> <main>{children}</main> </body> </html> </OllioProvider> ) }
App router
Page router
app/layout.tsx
import { OllioProvider, SignInButton, SignedIn, SignedOut, UserButton } from '@ollio/nextjs' import './globals.css' export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <OllioProvider> <html lang="en"> <body> <header> <SignedOut> <SignInButton /> </SignedOut> <SignedIn> <UserButton /> </SignedIn> </header> <main>{children}</main> </body> </html> </OllioProvider> ) }
5
Create your first user
5
Create your first user
Run your project with the following command:
Run your project with the following command:
npm
yarn
pnpm
terminal
npm run dev
npm
yarn
pnpm
terminal
npm run dev
What did you think of this content?
It was helpful
It was not helpful
I have feedback
What did you think of this content?
Helpful
Not helpful
Feedback
Last updated on
Dec
4,
2024
Last updated on
Dec
4,
2024