Next.js App Router - Server Component Functions - PropelAuth Docs

Server Component Functions

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

getUserOrRedirect

This function returns a user object, if the user is logged in. Otherwise, it redirects the user to the login page.

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, the middleware will automatically refresh it.

Optional Arguments

Example

import {getUserOrRedirect} from "@propelauth/nextjs/server/app-router";

const WelcomeMessage = async () => {
    // If the user is not logged in, they will be redirected to the login page
    const user = await getUserOrRedirect({ returnToPath: "/foo" })

return <div>Hello {user.firstName}!</div>
}

getUser

This function returns a user object, if the user is logged in. Otherwise, it returns undefined.

Example

import {getUser} from "@propelauth/nextjs/server/app-router";

const WelcomeMessage = async () => {
    const user = await getUser()

if (user) {
        return <div>Hello {user.firstName}!</div>
    } else {
        return <div>Please log in to be welcomed</div>
    }
}

getCurrentPath

Not compatible with Next.js 15 or later. Use getCurrentPathAsync instead.

Gets the path of the current page. This isn't technically a feature of this library, but it's currently a limitation of Next.js that it's difficult to get the current path from a server component. Since we needed to build that anyway in order to support redirects in getUserOrRedirect, we decided to provide this as a utility.

import {getCurrentPath} from "@propelauth/nextjs/server/app-router";

const path = getCurrentPath()
console.log(path)
// returns "/foo"

getCurrentPathAsync

import {getCurrentPathAsync} from "@propelauth/nextjs/server/app-router";

const path = await getCurrentPathAsync()
console.log(path)
// returns "/foo"

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)

getAccessToken

Not compatible with Next.js 15 or later. Use getAccessTokenAsync instead.

Gets the user's access token. This can be useful if making requests to a separate backend.

import {getAccessToken} from "@propelauth/nextjs/server/app-router";

const accessToken = getAccessToken()

getAccessTokenAsync

Gets the user's access token. This can be useful if making requests to a separate backend.

import {getAccessTokenAsync} from "@propelauth/nextjs/server/app-router";

const accessToken = await getAccessTokenAsync()

getDefaultActiveOrgId

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