• Backend Libraries

  • APIs

  • Frontend Libraries

  • Fullstack Libraries

  • Next.js App RouterRemix
  • Backend Libraries

  • Sign in
  • Server Component Functions

    Functions in this section are only useable in Next.js server side components

    getUserFromServerSideProps

    This function takes in the context object passed in to getServerSideProps and returns a user object, if the user is logged in. Otherwise, it returns undefined.

    Most of the time, validating the user will take no time and make no external requests. This is because the AuthProvider on the frontend keeps the user's information up to date periodically and on key events. However, if the user's information is stale, this function will automatically refresh it.

    If you want to guarantee that the user's information is fresh (useful for sensitive routes), you can use forceRefresh, the second argument.

    Example

    export async function getServerSideProps(context) {
        const user = await getUserFromServerSideProps(
            context, 
            false // forceRefresh
        )
    
        // Redirect to login if the user is not logged in
        if (!user) {
            return {
                redirect: {
                    destination: '/api/auth/login',
                    permanent: false,
                },
            }
        }
    
        return {
            props: {user},
        }
    }
    

    getUserFromApiRouteRequest

    This function takes in the req and res objects passed in to an API Route and returns a user object, if the user is logged in. Otherwise, it returns undefined.

    Most of the time, validating the user will take no time and make no external requests. This is because the AuthProvider on the frontend keeps the user's information up to date periodically and on key events. However, if the user's information is stale, this function will automatically refresh it.

    If you want to guarantee that the user's information is fresh (useful for sensitive routes), you can use forceRefresh, the third argument.

    Example

    import {NextApiRequest, NextApiResponse} from "next";
    import {getUserFromApiRouteRequest} from "@propelauth/nextjs/server/pages";
    
    export default async function handler(
        req: NextApiRequest, 
        res: NextApiResponse
    ) {
        const user = await getUserFromApiRouteRequest(req, res, false)
        if (user) {
            res.status(200).json({email: user.email})
        } else {
            res.status(401).json({error: "unauthorized"})
        }
    }
    

    getAuthInfoFromServerSideProps

    This function takes in the context object passed in to getServerSideProps and returns a user object as well as the user's access token, if the user is logged in. Otherwise, both the user object and access token are returned undefined.

    Most of the time, validating the user will take no time and make no external requests. This is because the AuthProvider on the frontend keeps the user's information up to date periodically and on key events. However, if the user's information is stale, this function will automatically refresh it.

    If you want to guarantee that the user's information is fresh (useful for sensitive routes), you can use forceRefresh, the second argument.

    Example

    export async function getServerSideProps(context) {
        const {user, accessToken} = await getAuthInfoFromServerSideProps(
            context, 
            false // forceRefresh
        )
    
        // Redirect to login if the user is not logged in
        if (!user) {
            return {
                redirect: {
                    destination: '/api/auth/login',
                    permanent: false,
                },
            }
        }
    
        return {
            props: {user},
        }
    }
    

    getAuthInfoFromApiRouteRequest

    This function takes in the req and res objects passed in to an API Route and returns a user object as well as the user's access token, if the user is logged in. Otherwise, it returns undefined.

    Most of the time, validating the user will take no time and make no external requests. This is because the AuthProvider on the frontend keeps the user's information up to date periodically and on key events. However, if the user's information is stale, this function will automatically refresh it.

    If you want to guarantee that the user's information is fresh (useful for sensitive routes), you can use forceRefresh, the third argument.

    Example

    import {NextApiRequest, NextApiResponse} from "next";
    import {getAuthInfoFromApiRouteRequest} from "@propelauth/nextjs/server/pages";
    
    export default async function handler(
        req: NextApiRequest, 
        res: NextApiResponse
    ) {
        const {user, accessToken} = await getAuthInfoFromApiRouteRequest(req, res, false)
        if (user) {
            res.status(200).json({email: user.email})
        } else {
            res.status(401).json({error: "unauthorized"})
        }
    }
    

    getPropelAuthApis

    Our APIs allow you to do things like block users, send invitations, and more. You can use our APIs like so:

    import {getPropelAuthApis} from "@propelauth/nextjs/server";
    
    // Can be done in an API route or getServerSideProps
    const apis = getPropelAuthApis()
    await apis.disableUser(userId)
    

    getDefaultActiveOrgId

    Allows you to to choose an activeOrgId when the user first visits your app. See the Active Org docs for more information.