> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getpara.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Session Lifecycle

> Managing the lifecycle of authentication sessions in Para

Learn how to check session status, maintain active sessions, and handle session expiration in Para web applications.

## Checking Session Status

Use `isSessionActive()` to verify whether a user's session is currently valid before performing authenticated operations.

<MethodDocs
  defaultExpanded={true}
  preventCollapse={true}
  name="isSessionActive()"
  description="Checks if the current session is active."
  async={true}
  returns={{
type: "boolean",
description: "True if the session is active."
}}
/>

Example usage:

```typescript theme={null}
const para = new Para(apiKey);

const isActive = await para.isSessionActive();

if (!isActive) {
  // Start a new authentication flow.
}
```

## Maintaining Active Sessions

Use `keepSessionAlive()` to extend an active session's validity without requiring full reauthentication.

<MethodDocs
  defaultExpanded={true}
  preventCollapse={true}
  name="keepSessionAlive()"
  description="Keeps the current session alive."
  async={true}
  returns={{
type: "boolean",
description: "True if the session was kept alive."
}}
/>

<Note>
  `keepSessionAlive()` is also the method to extend a session that was imported into the Server SDK.
</Note>

Example usage:

```typescript theme={null}
const para = new Para(apiKey);

const success = await para.keepSessionAlive();

if (!success) {
  // Start a new authentication flow.
}
```

### Automatic Session Management with React

If you're using the React SDK and the `ParaProvider` component, you can leverage automatic session management:

```typescript theme={null}
// The ParaProvider will automatically keep sessions alive by default
<ParaProvider>
  <YourApp />
</ParaProvider>

// To disable automatic session management
<ParaProvider
  config={{
    disableAutoSessionKeepAlive: true,
  }}
>
  <YourApp />
</ParaProvider>
```

<Info>
  When using the ParaProvider component from the React SDK, it automatically keeps sessions alive in the background by calling `keepSessionAlive()` periodically. You can disable this behavior by setting `config.disableAutoSessionKeepAlive` to `true` if you prefer to manage sessions manually.
</Info>

## Refreshing Sessions

Para provides the `refreshSession()` method for flows that intentionally send the user through a session refresh or login URL.

<MethodDocs
  defaultExpanded={true}
  preventCollapse={true}
  name="refreshSession()"
  description="Refreshes the current session."
  async={true}
  parameters={[
{
  name: "shouldOpenPopup",
  type: "boolean",
  optional: true
}
]}
  returns={{
type: "string",
description: "The refreshed session identifier."
}}
/>

<Info>
  `refreshSession()` is different from `keepSessionAlive()`. Use `keepSessionAlive()` to extend an active session. Use `refreshSession()` only when your app is starting a refresh or login flow for the user.
</Info>

For most applications, when a session expires, it's better to guide users through a complete authentication process:

```typescript theme={null}
const para = new Para(apiKey);

// When session expires, initiate a full authentication
if (!(await para.isSessionActive())) {
  //route to authentication page
}
```

<Warning>
  For server-side imported sessions, call `keepSessionAlive()` before expiry. If it fails, ask the client to authenticate again and export a new session.
</Warning>

## Best Practices

* **Proactive Session Management**: Always check session status before operations that require authentication
* **Regular Session Extension**: For long user sessions, periodically call `keepSessionAlive()` or leverage the `ParaProvider` automatic session management
* **Graceful Expiration Handling**: Provide a smooth re-authentication flow when sessions expire instead of showing errors
