# Authentication for Next.js and Postgres | Volcano

URL: https://volcano.dev/features/authentication

> Add email, Google, GitHub, Microsoft, Apple or guest login. The same user ID works in Postgres row-level security, functions and file storage.

## Connect login to your data

Users sign in with email, OAuth or as guests, and Postgres, functions and storage all see the same user ID.

Image: The Your Users page in the Volcano dashboard: user metrics, sign-in charts and the list of users.

## Keep each user’s data behind a login

One user ID reaches every policy and handler, so each rule checks the same person.

- **Sign-in**: Email, Google, GitHub, Microsoft, Apple and guests.
- **Identity**: Postgres policies and function handlers get the same user ID.
- **Sessions**: List sessions and sign out other devices.

## What you need

- **A Volcano account**: Auth is built in.
- **Volcano CLI**: Deploys your auth settings.
- **A Postgres table**: For policies that use the user ID.

## Try it

### 1. Sign users in

Sign-in works right away, or after email confirmation if you turn it on.

Client

```typescript
const volcano = new VolcanoAuth({ apiUrl, anonKey });

const { confirmationRequired } = await volcano.auth.signUp({ email, password });
if (!confirmationRequired) await volcano.auth.signIn({ email, password });
volcano.auth.signInWithGoogle();
```

### 2. Protect rows with a policy

auth.uid() is the signed-in user's ID.

Policy

```sql
alter table documents enable row level security;

create policy documents_owner_read on documents
  for select to authenticated using (owner_id = auth.uid());
```

### 3. Read the user in a function

Calls made with only the anon key have no user.

Function

```javascript
exports.handler = async (event) => {
  const auth = event.__volcano_auth;
  if (!auth) return { statusCode: 401 };
  return { statusCode: 200, body: JSON.stringify({ user_id: auth.user_id }) };
};
```

## Check it works

- Sign in as two different users. Each sees only their own rows.
- Call the function signed out. It returns 401.
- Sign out another device from the sessions list. That device’s session ends.

## Works with

- [Postgres](https://volcano.dev/features/postgres): Row-level policies use auth.uid().
- [Functions](https://volcano.dev/features/edge-functions): Handlers receive the verified user.
- [File storage](https://volcano.dev/features/file-storage): Scope uploads and reads per user.
- [Next.js frontends](https://volcano.dev/features/nextjs-hosting): Protect routes in middleware.

## Frequently asked questions

### What login methods does Volcano support?

Email and password, Google, GitHub, Microsoft and Apple OAuth, and guest sessions. Magic links, SMS, passkeys, TOTP and SAML aren’t supported.

### How do I add authentication to a Next.js app?

Sign users in with @volcano.dev/sdk. In middleware, call withAuth and redirect to your login page when it returns no user.

### How does authentication work with Postgres row-level security?

Policies call auth.uid() to get the signed-in user. Compare it to each row’s user\_id. Keep the service key on the server, since it bypasses RLS.

## Get started

Build with Authentication. Create a free account at https://volcano.dev/signup, or have your coding agent follow the setup playbook at https://volcano.dev/setup.md.
