Server Component Functions
Functions in this section are only useable 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
- Name
returnToPath
- Type
- string
- Description
An optional argument to specify where the user should be redirected to after they login. If not specified, the user will be redirected to your Application URL.
- Name
returnToCurrentPath
- Type
- boolean
- Description
An optional argument to redirect the user to the current page after they log in. If not specified or set to
false
, the user will be redirected to your Application URL.
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.
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.
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
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
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 {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
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 to choose an activeOrgId when the user first visits your app. See the Active Org docs for more information.