Web
Next.js
Integrate Rownd instant accounts and authentication into your Next.js project, including support for server-side rendering
Quick Navigation
Section | Description |
---|---|
Installation | How to install the Next.js SDK |
Basic Setup | Setting up providers and middleware |
Server-Side Features | Server components and authentication |
Client-Side Features | Client components and hooks |
Authentication Flow | Complete auth implementation |
Protected Routes | Route protection strategies |
Data Access | Accessing user data (server/client) |
Best Practices | Recommended patterns |
Type Definitions | TypeScript interfaces |
Examples | Common use cases |
Installation
Basic Setup
1. Provider Setup
In your root layout.tsx
:
2. Middleware Setup
In your middleware.ts
:
Server-Side Features
The @rownd/next/server
package provides server-side authentication utilities:
Server-Side Functions
Function | Description | Parameters | Return Type | Usage Example |
---|---|---|---|---|
getRowndUser | Retrieves the current authenticated user’s data from Rownd | cookies: ReadonlyRequestCookies | Promise<RowndServerUser | null> | const user = await getRowndUser(cookies) |
getRowndUserId | Gets the current user’s ID from server context | cookies: ReadonlyRequestCookies | Promise<string | null> | const userId = await getRowndUserId(cookies) |
getRowndAccessToken | Retrieves the current access token from server context | cookies: ReadonlyRequestCookies | Promise<string | null> | const token = await getRowndAccessToken(cookies) |
isAuthenticated | Checks if there is an authenticated user in server context | cookies: ReadonlyRequestCookies | Promise<boolean> | const isAuth = await isAuthenticated(cookies) |
withRowndMiddleware | Higher-order function for Next.js middleware to handle authentication | middleware: NextMiddleware | NextMiddleware | See middleware example below |
withRowndRequireSignIn | Higher-order function to protect routes/pages/API handlers | handler: Function, cookies: ReadonlyRequestCookies | Function | See protected routes example below |
Example Usage
Client-Side Features
The client-side features mirror the React SDK functionality but must be used within client components:
The useRownd Hook
The useRownd
hook provides comprehensive authentication and user management functionality:
State Properties
Property | Type | Description | Example Usage | |||||
---|---|---|---|---|---|---|---|---|
is_initializing | boolean | Whether Rownd is still loading | if (is_initializing) return <Loading /> | |||||
is_authenticated | boolean | Whether user is currently signed in | if (is_authenticated) showDashboard() | |||||
access_token | string | null | Current JWT access token | headers: { Authorization: Bearer ${access_token} } | |||||
user.data | Record<string, any> | User’s profile data | const { first_name, email } = user.data | user.groups | string[] | Groups the user belongs to | if (user.groups.includes('admin')) |
Authentication Methods
Method | Description | Parameters | Return Type |
---|---|---|---|
requestSignIn() | Triggers sign-in flow | { auto_sign_in?: boolean, identifier?: string, method?: string, post_login_redirect?: string } | void |
signOut() | Signs out current user | None | void |
getAccessToken() | Gets current access token | { waitForToken?: boolean, timeoutMs?: number } | Promise<string> |
User Data Methods
Method | Description | Parameters | Return Type |
---|---|---|---|
setUser() | Updates multiple user fields | Record<string, any> | Promise<void> |
setUserValue() | Updates a single user field | (field: string, value: any) | Promise<void> |
manageAccount() | Opens account management UI | None | void |
Additional Features
Feature | Description | Parameters | Return Type |
---|---|---|---|
getFirebaseIdToken() | Gets Firebase ID token | None | Promise<string> |
getAppConfig() | Gets app configuration | None | Promise<AppConfig> |
passkeys | Passkey authentication methods | { register: () => Promise<void>, authenticate: () => Promise<void> } | Object |
events | Event system for state changes | addEventListener(event: string, callback: Function) | EventEmitter |
Example Usage
Authentication Flow
Combined Server/Client Authentication
Protected Routes
Server-Side Protection
Component-Level Protection
Data Access
Server-Side Data Access
Client-Side Data Access
Best Practices
-
Server/Client Separation
-
Efficient Token Handling
-
Error Boundaries
Type Definitions
Core Types
Type | Description | Interface |
---|---|---|
RowndServerUser | Server-side user type | { data: Record<string, any>; access_token: string; app_id: string; user_id: string; } |
RowndUser | Client-side user type | { data: Record<string, any>; groups: string[]; redacted_fields: string[]; verified_data: Record<string, any>; meta: UserMeta; } |
UserMeta | User metadata | { created_at: string; updated_at: string; last_sign_in: string; } |
SignInOptions | Sign-in configuration | { auto_sign_in?: boolean; identifier?: string; method?: AuthMethod; post_login_redirect?: string; } |
TokenOptions | Token retrieval options | { waitForToken?: boolean; timeoutMs?: number; } |
AuthMethod | Authentication methods | 'email' | 'phone' | 'google' | 'apple' | 'passkey' | 'anonymous' |
Server-Side Types
Client-Side Types
TypeScript Examples
Server-Side Example
Client-Side Example
Combined Server/Client Example
Examples
Full Authentication Flow
For more examples and detailed client-side features, refer to the React SDK documentation.