Integration Guide
NextJS
Exports
API

API

Server Only API

import {
  fetchCurrentUser,
  fetchOrganizations,
  fetchOrganizationMembers,
  fetchOrganizationMembership,
  updateCurrentUser,
} from '@akarigar/auth/server';
function fetchCurrentUser(): Promise<User | null>;
function updateCurrentUser(data: UpdateUserInput): Promise<User | null>;
function fetchOrganizations(): Promise<Organization[]>;
function fetchOrganizationMembers(
  organizationId: string,
): Promise<OrganizationMember[]>;
function fetchOrganizationMembership(
  organizationId: string,
): Promise<OrganizationMember | null>;
fetchCurrentUser

The fetchCurrentUser function retrieves information about the currently authenticated user.

import { fetchCurrentUser } from '@akarigar/auth/server';
 
// In a server component
const user = await fetchCurrentUser();
 
if (user) {
  // User is authenticated
  console.log(`Logged in as: ${user.firstName} ${user.lastName}`);
} else {
  // User is not authenticated
}
updateCurrentUser

The updateCurrentUser function allows you to update the current user's information, particularly their server metadata.

import { updateCurrentUser } from '@akarigar/auth/server';
import type { UpdateUserInput } from '@akarigar/auth/server';
 
// Example: Update server metadata
await updateCurrentUser({
  serverMetadata: {
    lastSeenNotification: '2025-04-20',
    preferredTheme: 'dark',
    stripeId: 'secret-id',
  },
});
 
// Reset server metadata by passing null
await updateCurrentUser({
  serverMetadata: null,
});

Server Metadata Rules:

  • Server metadata keys cannot include . (dot) characters
  • Server metadata keys cannot include null (\0) characters
  • Providing null for data.serverMetadata will reset the server metadata completely
  • Invalid keys will be automatically filtered out during the update process
fetchOrganizations

The fetchOrganizations function retrieves all organizations that the current user is a member of.

import { fetchOrganizations } from '@akarigar/auth/server';
 
// In a server component
const organizations = await fetchOrganizations();
 
// Display user's organizations
<ul>
  {organizations.map((org) => (
    <li key={org.id}>{org.name}</li>
  ))}
</ul>;

Note: The current user must be logged in to view their organizations.

fetchOrganizationMembers

The fetchOrganizationMembers function retrieves all members of a specified organization.

import { fetchOrganizationMembers } from '@akarigar/auth/server';
 
// In a server component
const organizationId = 'org_123456789';
const members = await fetchOrganizationMembers(organizationId);
 
// Display organization members
<ul>
  {members.map((member) => (
    <li key={member.id}>
      {member.user.firstName} {member.user.lastName} - {member.role}
    </li>
  ))}
</ul>;

Parameters:

  • organizationId (string): The ID of the organization to fetch members for.

Note: The current user must be a member of the organization to view its members.

fetchOrganizationMembership

The fetchOrganizationMembership function retrieves the current user's membership information in a specified organization.

import { fetchOrganizationMembership } from '@akarigar/auth/server';
 
// In a server component
const organizationId = 'org_123456789';
const membership = await fetchOrganizationMembership(organizationId);
 
if (membership) {
  // User is a member of this organization
  console.log(`Role in organization: ${membership.role}`);
} else {
  // User is not a member of this organization
}

Parameters:

  • organizationId (string): The ID of the organization to fetch membership information for.

Returns: The membership information of the current user in the specified organization, or null if the user is not a member.