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 call
  • currentUser() — 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

FieldTypeDescription
userIdstringUser UUID
sessionIdstringSession UUID
rolestringUser's role (e.g., "viewer", "admin")
expiresAtnumberToken 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

FieldTypeDescription
idstringUser UUID
emailstringEmail address
emailVerifiedbooleanWhether email is verified
namestring | nullDisplay name
avatarUrlstring | nullAvatar URL
rolestringUser's role
metadataRecord<string, any>Custom metadata

When to use which

getSession()currentUser()
SpeedFast (no network)Slower (API call)
DatauserId, role, sessionIdFull profile
Setup requiredTier 1 (minimal)Tier 2 (full)
Use caseRoute guards, role checksProfile pages, personalization

Configuration reference

OptionTypeDefaultDescription
projectIdstringrequiredProject UUID for JWKS
apiUrlstring"https://api.vibelogin.com"API base URL
basePathstring"/api/auth"Auth handler path
cookiePrefixstring"vibeauth"Cookie name prefix