Auth SSR

Learn how to use SSR with Firebase Authentication and Next.js

MakerKit uses SSR throughout the website. If you choose to use SSR, you can persist the user's authentication on every page.

Whether SSR on every page is something your website may need or not is up to your needs, but MakerKit makes this process very easy.

Let's assume the following is your Next.js Index page, and we want to show the user's Profile avatar if they signed in. You will need to fetch the user's data from the getServerSideProps function and return it as props to the current page.

The MakerKit default template does it for you, but you need to remember that you need to add the following snippet to every page you want the authentication persisted.

import { withUserProps } from '~/app/with-user-props';

function Index({ user }) {
    // display user in your components
}

export function getServerSideProps(ctx: GetServerSidePropsContext) {
  return withUserProps(ctx);
}

MakerKit also provides a UserContext context, which you can inject with useContext in any of your components. Assuming you have a component AvatarComponent in which you want to display your user's avatar:

function ProfileAvatar() {
    const { user } = useContext(UserContext);

    return (
        <img src={user.profileURL} alt='Profile Photo' />
    )
}

Alternatively, you can use the hook useUserSession:

function ProfileAvatar() {
    const {auth} = useUserSession();

    return (
        <img src={auth.profileURL} alt='Profile Photo' />
    )
}

Stay informed with our latest resources for building a SaaS

Subscribe to our newsletter to receive updatesor