• 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
    • Adding API Routes
    • Change Authentication strategy
    • Fetching the signed in User
    • Reading a Document
    • Creating a Document
    • Configuring Plans
    • Project Configuration
    • Updating the Navigation menu
    • Adding a new translation string
    • Guarding an API Route
    • Adding Pages
    • Updating the Sidebar menu
    • Require Email Verification
    • Fetching the selected Organization
    • Reading a list of Documents
    • Updating a Document
    • Running the Stripe Webhook locally
    • Branding
    • Setting a Default Language
    • Dark Theme
    • Theming
    • Calling API Routes from the client
    • Deleting a Document
    • Updating the Logo
    • Adding a new language in the Next.js Firebase SaaS Kit
    • Checking CSRF Tokens
    • Passing data from server to client
    • Updating the Fonts
    • Adding Pages
    • Guarding Pages
    • Using Lemon Squeezy instead of Stripe
    • Updating the Favicons
    • Using the Language Switcher
    • Environment variables
    • Detect current Locale
    • Setting up Emails

Fetching the selected Organization

Learn how to fetch the selected organization from the backend and frontend.

There are several ways to fetch the current selected organization, depending on the use-case.

Fetching the selected Organization from the backend

You will use this function fairly often for fetching the current organization from the backend.

tsx
import { NextApiRequest,NextApiResponse } from "next";
import { getCurrentOrganization } from '~/lib/server/organizations/get-current-organization';
import { withPipe } from '~/core/middleware/with-pipe';
import { withAuthedUser } from '~/core/middleware/with-authed-user';
async function handler(req: NextApiRequest, res: NextApiResponse) {
const user = req.firebaseUser;
const organization = await getCurrentOrganization(user.uid);
// ...
}
export default withPipe(withAuthedUser, handler);

Fetching the selected Organization from the frontend

The withAppProps middleware used in the gated app pages makes sure that the current organization is passed from the backend to the frontend.

Getting the Organization with "useCurrentOrganization"

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

tsx
import { useCurrentOrganization } from '~/lib/organizations/hooks/use-current-organization';
const organization = useCurrentOrganization();

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

Getting if the Organization subscription status is set to "active" on the frontend

You can use the useIsSubscriptionActive hook to get the subscription status of the current organization. You may want to use this function to gate access to certain parts of the app.

tsx
const isActive = useIsSubscriptionActive();

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

On this page
  1. Fetching the selected Organization from the backend
    1. Fetching the selected Organization from the frontend
      1. Getting the Organization with "useCurrentOrganization"
      2. Getting if the Organization subscription status is set to "active" on the frontend