# Sign In, Sign Out, and Redirects (/docs/sdks/nextjs/redirects)

This guide shows how to trigger **sign-in, sign-out, and sign-up flows** in a Next.js application using MonoCloud.

MonoCloud provides two ways to start authentication flows:

- **Client components** for declarative UI
- **Redirect helpers** for imperative navigation (server-only)

Use these helpers when you want to explicitly control authentication flows rather than relying on automatic redirects from protected routes.

## When to use these helpers [#when-to-use-these-helpers]

Use these APIs when:

- You need manual control over the authentication flow
- You want explicit sign-in or sign-out buttons
- You want to redirect users based on business logic
- You’re building custom onboarding or logout flows

## Auth Action Components & Redirects [#auth-action-components--redirects]

MonoCloud provides **UI components** for common authentication actions such as signing in, signing out, and signing up.

These components:
- `<SignIn />`, `<SignUp />`, and `<SignOut />` can be rendered from both **Server Components** and **Client Components**
- `<RedirectToSignIn />` is a **Client Component only**
- Perform redirects automatically
- Require no **event handlers**
- Work in both **App Router** and **Pages Router**

### &lt;SignIn /&gt; [#signin-]

`<SignIn />` renders a clickable element that redirects the user to sign in.

```tsx title="src/components/sign-in-button.tsx"
import { SignIn } from "@monocloud/auth-nextjs/components";

export function SignInButton() {
  return <SignIn>Sign in</SignIn>;
}
```

**With a return URL**

```tsx title="tsx"
<SignIn returnUrl="/dashboard">Sign in</SignIn>
```

**Behavior**

- Redirects the user to MonoCloud-hosted sign-in page
- Automatically returns the user to the current page (or returnUrl)
- Respects middleware and return URL configuration

### &lt;SignUp /&gt; [#signup-]

`<SignUp />` redirects users to the MonoCloud-hosted sign-up flow.

```tsx title="src/components/sign-up-button.tsx"
import { SignUp } from "@monocloud/auth-nextjs/components";

export function SignUpButton() {
  return <SignUp>Create account</SignUp>;
}
```

Use this when you want to send users directly to registration instead of sign-in.

### &lt;SignOut /&gt; [#signout-]

`<SignOut />` signs the user out and redirects them to the configured post-logout URL.

```tsx title="src/components/sign-out-button.tsx"
import { SignOut } from "@monocloud/auth-nextjs/components";

export function SignOutButton() {
  return <SignOut>Sign out</SignOut>;
}
```

**Behavior**

- Clears the MonoCloud session
- Redirects the user back to your application
- Respects configured sign-out URLs

### &lt;RedirectToSignIn /&gt; [#redirecttosignin-]

`<RedirectToSignIn />` immediately redirects the user to the MonoCloud-hosted sign-in page when rendered.

`<RedirectToSignIn />` is a **Client Component only** and must be rendered from a file with `"use client"`.

Use this when:
- You want to redirect as part of render logic
- You don’t need a clickable button
- You’re handling access-denied or empty states

```tsx title="src/components/require-auth.tsx"
"use client";

import { RedirectToSignIn } from "@monocloud/auth-nextjs/components/client";

export function RequireAuth() {
  return <RedirectToSignIn />;
}
```

**With a return URL**

```tsx title="tsx"
<RedirectToSignIn returnUrl="/dashboard" />
```

**Behavior**
- Redirects immediately when rendered
- Does not render UI
- Does not throw
- Does not require middleware checks

## Imperative redirects (App Router only, server-side) [#imperative-redirects-app-router-only-server-side]

For full control over navigation, use the redirect helpers provided by the SDK.

These helpers work **only in the Next.js App Router** and can be used from:

- Server Components
- Server Actions
- API routes

### redirectToSignIn() [#redirecttosignin]

Redirects the user to the MonoCloud sign-in flow.

> This helper is **only supported in the Next.js App Router**.

```tsx title="tsx"
await redirectToSignIn();
```

**Common use cases**

- Conditional redirects
- Server Actions
- Optional authentication flows

### redirectToSignOut() [#redirecttosignout]

Signs the user out and redirects them back to your app.

> This helper is **only supported in the Next.js App Router**.

```tsx title="tsx"
await redirectToSignOut();
```

Use this when sign-out is triggered from logic instead of UI.

### Customize the return URL [#customize-the-return-url]

Both helpers accept options to override where the user is sent after authentication.

```tsx title="tsx"
await redirectToSignIn({
  returnUrl: "/dashboard",
});
```

```tsx title="tsx"
await redirectToSignOut({
  returnUrl: "/dashboard",
});
```

### Redirecting from a Server Action [#redirecting-from-a-server-action]

```tsx title="src/app/actions.ts"
"use server";

import { redirectToSignIn } from "@monocloud/auth-nextjs";

export async function startSignIn() {
  await redirectToSignIn({
    returnUrl: "/dashboard",
  });
}
```

The Server Action does not complete—the redirect happens immediately.

## When to use components vs redirect helpers [#when-to-use-components-vs-redirect-helpers]

| Use case                     | Recommended                                 |
| ---------------------------- | ------------------------------------------- |
| Clickable sign-in button     | `<SignIn />`                                |
| Clickable sign-up button     | `<SignUp />`                                |
| Clickable sign-out button    | `<SignOut />`                               |
| Immediate redirect on render | `<RedirectToSignIn />`                      |
| Server-side redirect         | `redirectToSignIn()`, `redirectToSignOut()` |

## Next steps [#next-steps]

Continue securing your application:

- App Router
  - [Protect server components](/docs/sdks/nextjs/app-router/protect-server-components)
  - [Protect client components](/docs/sdks/nextjs/app-router/protect-client-components)
  - [Protect API routes](/docs/sdks/nextjs/app-router/protect-api-routes)
  - [Protect server actions](/docs/sdks/nextjs/app-router/protect-server-actions)

- Pages Router
  - [Protect server rendered pages](/docs/sdks/nextjs/page-router/protect-ssr-page)
  - [Protect client rendered pages](/docs/sdks/nextjs/page-router/protect-csr-page)
  - [Protect API routes](/docs/sdks/nextjs/page-router/protect-api-route)
