• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License
  • Global Configuration
    • Environment Variables
    • Feature Flags
  • Server Actions
    • Sending CSRF Token to Actions
    • Server Actions Error Handling
  • The Makerkit SDK
    • User SDK
    • Organization SDK
    • Organization Subscription SDK
    • Data Loader SDK
  • Architecture and Folder Structure
    • Structure your Application
    • Data Model
This documentation is for a legacy version of Next.js and Supabase. For the latest version, please visit the Next.js and Supabase V2 documentation

User SDK - Fetch the current User | Next.js Supabase SaaS Starter Kit

Learn how to use the User SDK to interact with the current user

The User SDK allows you to interact with the current user. Here we list the methods available in the User SDK.

The example below work within a Server Components: by using the required client, you can easily adapt the code examples below to both Server Actions and Route Handlers.

The User SDK is namespaced under sdk.user - so all the methods below are available under sdk.user.

Get the current user

To get the current user, use the sdk.user.getCurrent() method:

tsx
import getSdk from '~/lib/sdk';
import getSupabaseServerComponentClient from '~/core/supabase/server-component-client';
async function PageServerComponent() {
const client = getSupabaseServerComponentClient();
const sdk = getSdk(client);
const user = await sdk.user.getCurrent();
// ...
}

This method wraps the Supabase auth.getUser() function.

Get the current user's session

To get the current session, use the sdk.user.getCurrentSession() method:

tsx
import getSdk from '~/lib/sdk';
import getSupabaseServerComponentClient from '~/core/supabase/server-component-client';
async function PageServerComponent() {
const client = getSupabaseServerComponentClient();
const sdk = getSdk(client);
const session = await sdk.user.getCurrentSession();
// ...
}

This method wraps the Supabase auth.getSession() function.

Get the current user's ID

To get the current user's ID, use the sdk.user.getCurrentId() method:

tsx
import getSdk from '~/lib/sdk';
import getSupabaseServerComponentClient from '~/core/supabase/server-component-client';
async function PageServerComponent() {
const client = getSupabaseServerComponentClient();
const sdk = getSdk(client);
const userId = await sdk.user.getCurrentId();
// ...
}

Get the current user's Database profile

We store the user's record data in the database table users. To get the current user's profile data, use the sdk.user.getData() method:

tsx
import getSdk from '~/lib/sdk';
import getSupabaseServerComponentClient from '~/core/supabase/server-component-client';
async function PageServerComponent() {
const client = getSupabaseServerComponentClient();
const sdk = getSdk(client);
const data = await sdk.user.getData();
// ...
}

The data object will contain the user's profile data.

tsx
interface UserData {
displayName: string | null;
photoUrl: string | null;
// your custom fields...
}

It's very likely that you'll be adding more fields, so this interface may change.

On this page
  1. Get the current user
    1. Get the current user's session
      1. Get the current user's ID
        1. Get the current user's Database profile