Server Helpers — getSession() & currentUser()
Overview
Server helpers let you access the authenticated user in server components and API routes. Two levels are available:
getSession() — fast JWT verification, no network callcurrentUser() — full user profile via API call
Setup
import { createHostedServerHelpers } from "@vibelogin/nextjs/hosted-server";
const { getSession, currentUser } = createHostedServerHelpers({
projectId: process.env.VIBELOGIN_PROJECT_ID!,
});
getSession()
Returns the JWT payload without making any network calls. Available with Tier 1 (minimal) setup.
// app/dashboard/page.tsx
export default async function Dashboard() {
const session = await getSession();
if (!session) redirect("/");
return <p>User ID: {session.userId}, Role: {session.role}</p>;
}
Return type HostedSessionIdentity
| Field | Type | Description |
|---|
| userId | string | User UUID |
| sessionId | string | Session UUID |
| role | string | User's role (e.g., "viewer", "admin") |
| expiresAt | number | Token expiry timestamp |
currentUser()
Returns the full user profile by calling the local auth proxy. Requires Tier 2 setup (hosted handler).
export default async function ProfilePage() {
const user = await currentUser();
if (!user) redirect("/");
return (
<div>
<h1>{user.name}</h1>
<p>{user.email}</p>
<p>Verified: {user.emailVerified ? "Yes" : "No"}</p>
</div>
);
}
Return type HostedSessionUser
| Field | Type | Description |
|---|
| id | string | User UUID |
| email | string | Email address |
| emailVerified | boolean | Whether email is verified |
| name | string | null | Display name |
| avatarUrl | string | null | Avatar URL |
| role | string | User's role |
| metadata | Record<string, any> | Custom metadata |
When to use which
| getSession() | currentUser() |
|---|
| Speed | Fast (no network) | Slower (API call) |
| Data | userId, role, sessionId | Full profile |
| Setup required | Tier 1 (minimal) | Tier 2 (full) |
| Use case | Route guards, role checks | Profile pages, personalization |
Configuration reference
| Option | Type | Default | Description |
|---|
| projectId | string | required | Project UUID for JWKS |
| apiUrl | string | "https://api.vibelogin.com" | API base URL |
| basePath | string | "/api/auth" | Auth handler path |
| cookiePrefix | string | "vibeauth" | Cookie name prefix |