# Server Component Functions

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

## [getUserOrRedirect](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-user-or-redirect)

This function returns a [user](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/user-orgmemberinfo#user-object) 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](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/installation-and-setup#auth-provider) 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

```js
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](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-user)

This function returns a [user](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/user-orgmemberinfo#user-object) object, if the user is logged in. Otherwise, it returns undefined.

### Example

```ts
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](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-current-path)

Not compatible with Next.js 15 or later. Use [getCurrentPathAsync](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-current-path-async) 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](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-user-or-redirect), we decided to provide this as a utility.

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

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

## [getCurrentPathAsync](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-current-path-async)

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

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

## [getPropelAuthApis](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-propel-auth-apis)

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

```typescript
import {getPropelAuthApis} from "@propelauth/nextjs/server";

// Can be done in an API route or getServerSideProps
const apis = getPropelAuthApis()
await apis.disableUser(userId)
```

## [getAccessToken](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-access-token)

Not compatible with Next.js 15 or later. Use [getAccessTokenAsync](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-access-token-async) instead.

Gets the user's [access token](https://docs.propelauth.com/recipes/access-tokens). This can be useful if making requests to a separate backend.

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

const accessToken = getAccessToken()
```

## [getAccessTokenAsync](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-access-token-async)

Gets the user's [access token](https://docs.propelauth.com/recipes/access-tokens). This can be useful if making requests to a separate backend.

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

const accessToken = await getAccessTokenAsync()
```

## [getDefaultActiveOrgId](https://docs.propelauth.com/reference/fullstack-apis/nextjsapp/server-component-functions?ref=propelauth.mymidnight.blog#get-default-active-org-id)

Allows you to choose an activeOrgId when the user first visits your app. See the [Active Org docs](https://docs.propelauth.com/recipes/active-org) for more information.
