# Server Component Functions

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

## [getUserFromServerSideProps](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/server-component-functions#get-user-from-server-side-props)

This function takes in the `context` object passed in to `getServerSideProps` and returns a [user](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/user-orgmemberinfo#user-object) 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](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/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, 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

```js
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](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/server-component-functions#get-user-from-api-route-request)

This function takes in the `req` and `res` objects passed in to an API Route and returns a [user](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/user-orgmemberinfo#user-object) object, if the user is logged in. Otherwise, it returns undefined.

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

### Example

```ts
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](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/server-component-functions#get-auth-info-from-server-side-props)

This function takes in the `context` object passed in to `getServerSideProps` and returns a [user](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/user-orgmemberinfo#user-object) object as well as the user's [access token](https://docs.propelauth.com/recipes/access-tokens), if the user is logged in. Otherwise, both the user object and access token are returned undefined.

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

### Example

```js
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](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/server-component-functions#get-auth-info-from-api-route-request)

This function takes in the `req` and `res` objects passed in to an API Route and returns a [user](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/user-orgmemberinfo#user-object) object as well as the user's [access token](https://docs.propelauth.com/recipes/access-tokens), if the user is logged in. Otherwise, it returns undefined.

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

### Example

```ts
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](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/server-component-functions#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)
```

---

## [getDefaultActiveOrgId](https://docs.propelauth.com/reference/fullstack-apis/nextjspages/server-component-functions#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.
