• 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
    • Adding API Routes
    • Change Authentication strategy
    • Fetching the signed in User
    • Reading Records from Postgres
    • Seeding Local Data
    • Introduction
    • Configuring Plans
    • Project Configuration
    • Updating the Navigation menu
    • Adding a new translation string
    • Supabase is not starting
    • Calling API Routes from the client
    • Adding Pages
    • Updating the Sidebar menu
    • Setup oAuth
    • Fetching the selected Organization
    • Resetting the local DB
    • Running the Stripe Webhook locally
    • Branding
    • Setting a Default Language
    • Supabase is not stopping
    • Dark Theme
    • Theming
    • Generating Database Types
    • Updating the Logo
    • Adding a new language in the Remix Supabase SaaS Kit
    • Tables/Functions not found
    • Updating the Fonts
    • Adding Pages
    • Adding a new translation file
    • Contentlayer gets stuck
    • Guarding Pages
    • Using Lemon Squeezy instead of Stripe
    • Updating the Favicons
    • Using the Language Switcher
    • Environment variables
    • Setting up Emails
This documentation is for a legacy version of Remix and Supabase. For the latest version, please visit the Remix and Supabase V2 documentation

Fetching the signed in User

Learn how to fetch the signed in user from the backend and frontend.

There are several ways to fetch the signed in user, depending on the use-case.

Fetching the signed in user from the backend

To do so, use the function requireSession, which also takes care of redirecting the user to the login page if they are not signed in, and checking the correct MFA access level if the user opted in.

You can use this function in:

  • API Routes
  • Server Actions
  • Server Components

Using "requireSession" in API Routes

When using API routes, you can retrieve the signed in user using the requireSession function.

tsx
import getSupabaseServerClient from '~/core/supabase/server-client';
import requireSession from '~/lib/user/require-session.server';
export async function POST() {
const client = getSupabaseServerClient();
const session = await requireSession(client);
}

Using "requireSession" in Loaders

When using Remix Loaders, you can retrieve the signed in user using the requireSession function.

tsx
import { LaaderFunctionArgs } from '@remix-run/node';
import getSupabaseServerClient from '~/core/supabase/server-client';
import requireSession from '~/lib/user/require-session.server';
export async function loader(args: LaaderFunctionArgs) {
const client = getSupabaseServerClient();
const session = await requireSession(client);
// use session here
}

Using "requireSession" Actions

You can use the requireSession function in Remix Actions to retrieve the signed in user.

tsx
import { ActionFunctionArgs } from '@remix-run/node';
export async function action(args: ActionFunctionArgs) {
const client = getSupabaseServerClient(args.request);
const session = await requireSession(client);
// use session here
}

Fetching the signed in user from the frontend

When in the scope of the _app layout, the app automatically injects the user session in the context layout.

This includes the user ID, email, and other authentication data, as well as the user's Supabase DB record.

Getting the user Session with "useUserSession"

To retrieve the signed in user from the frontend, you can use the useUserSession hook:

tsx
import useUserSession from '~/core/hooks/use-user-session';
const userSession = useUserSession();

This is a React hook and can only be used inside a React component.

Getting the user ID with "useUserId"

To retrieve the signed in user ID from the frontend, you can use the useUserId hook:

tsx
import useUserId from '~/core/hooks/use-user-id';
const userId = useUserId();

This is a React hook and can only be used inside a React component.

Getting the user role within the organization with "useUserRole"

To retrieve the signed in user role of the current organization from the frontend, you can use the useUserRole hook:

tsx
import useCurrentUserRole from '~/core/hooks/use-current-user-role';
const role = useCurrentUserRole();

This is a React hook and can only be used inside a React component.

On this page
  1. Fetching the signed in user from the backend
    1. Using "requireSession" in API Routes
    2. Using "requireSession" in Loaders
    3. Using "requireSession" Actions
  2. Fetching the signed in user from the frontend
    1. Getting the user Session with "useUserSession"
    2. Getting the user ID with "useUserId"
    3. Getting the user role within the organization with "useUserRole"