• 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

Guarding Pages in the Next.js Firebase SaaS kit

Learn how to guard pages in your Next.js Firebase application

As a server-side rendered application, Next.js Firebase will always render the page on the server before sending it to the client. This means that you can use the getServerSideProps function to guard pages from being accessed by unauthorized users.

As shown before, we will reuse the withAppProps function to make sure that we initialize the required data before rendering the page.

In the example below:

  1. We use the withAppProps function to initialize the required data
  2. We check if the user can access the page
  3. If the user can't access the page, we redirect them to the dashboard

This is a common pattern that you can use to guard pages in your application and redirect users to the dashboard if they are not authorized to access the page.

tsx
import { GetServerSidePropsContext } from "next";
import { withAppProps } from '~/lib/props/with-app-props';
function Page() {
// render the page
}
export async function getServerSideProps(
ctx: GetServerSidePropsContext
) {
const appProps = await withAppProps(ctx);
// We augment the props with the data we want
// to pass to the client
// replace the below with your own logic
const canAccessPage = await checkUserCanAccessPage();
if (!canAccessPage) {
return {
redirect: {
destination: '/dashboard',
permanent: false,
},
}
}
return appProps;
}

Guarding Application Pages with User Roles

Let's make a more complete example by checking the user role and redirecting them to the dashboard if they are not an admin.

tsx
import { GetServerSidePropsContext } from "next";
import { withAppProps } from '~/lib/props/with-app-props';
import { MembershipRole } from '~/lib/organizations/types/membership-role';
function Page() {
// render the page
}
export async function getServerSideProps(
ctx: GetServerSidePropsContext
) {
const appProps = await withAppProps(ctx);
// get user role within the organization
const userRole = appProps.organization.members[appProps.user.id].role;
if (!canAccessPage(userRole)) {
return {
redirect: {
destination: '/dashboard',
permanent: false,
},
}
}
return appProps;
}
function canAccessPage(role: MembershipRole) {
return role >= MembershipRole.Admin;
}

Guarding Application Pages with Organization Subscriptions

In this example, we want to make sure the Organization is subscribed to a plan before allowing the user to access the page.

To do so, we check the subscription property of the Organization object and redirect the user to the dashboard if the subscription is active or trialing.

tsx
import { GetServerSidePropsContext } from "next";
import { withAppProps } from '~/lib/props/with-app-props';
import { OrganizationSubscription } from '~/lib/organizations/types/organization-subscription';
function Page() {
// render the page
}
export async function getServerSideProps(
ctx: GetServerSidePropsContext
) {
const appProps = await withAppProps(ctx);
const subscription = appProps.organization.subscription;
if (!canAccessPage(subscription)) {
return {
redirect: {
destination: '/dashboard',
permanent: false,
},
}
}
return appProps;
}
function canAccessPage(subscription: OrganizationSubscription) {
return ['active', 'trialing'].includes(subscription.status);
}
On this page
  1. Guarding Application Pages with User Roles
    1. Guarding Application Pages with Organization Subscriptions