

# Manage users

Amplify backend auth stores your users in the `auth."user"` table. You can read the table with SQL, but you can’t change it with SQL or a migration. To create, find, change and delete users, run the `aws amplifygen3 backend user` commands, or call `createServerAdmin` from your server code.

## Choose a tool
<a name="backend-users-tool"></a>
+  **The CLI** – `aws amplifygen3 backend user create`, `list`, `get`, `edit` and `delete` work on a deployed environment. `aws amplifygen3 local backend user` takes the same verbs for the local backend, and needs no AWS credentials.
+  **Server code** – `createServerAdmin` signs each call with your server’s AWS credentials, and needs no key. Because it runs as `service_role` and can manage every user, use it only in server code, never in browser code.

The deployed commands work on `preview` by default. To work on another environment, pass `--env`, such as `--env production`, or pass `--branch-arn`. They sign each request with your AWS credentials. Use credentials from the account that owns the app, with permission for `amplifygen3:Connect` on the branch. Outside the app’s folder, add `--app`. The local commands don’t take `--app`, `--env` or `--branch-arn`. They work on the local backend that `aws amplifygen3 local run` started. In `production`, the commands change the accounts of your real users. If an environment has more than one branch, the command asks you to pick one, or you can pass `--branch-arn`. When `CI=true` is set and you leave it out, the command exits with code `252` and lists the branch ARNs.

```typescript
import { createServerAdmin } from '@aws-amplifygen3/auth-js/server'

const admin = createServerAdmin({ baseUrl: `${process.env.AMPLIFY_BACKEND_URL}/auth` })
```

Each call returns `{ data, error }`, on the local backend and on a deployed one. `createServerAdmin` takes AWS credentials from the default credential chain, or from `awsCredentialsProvider` if you pass it. The credentials must belong to the backend’s AWS account and allow `amplifygen3:Connect`. `createServerAdmin` throws if `baseUrl` is missing or isn’t an `https://` URL. An `http://` URL works only for a local backend. For the permission that deployed code needs, see [Permissions for your app](add-IAM-roles.md).

**Note**  
 `createServerAdmin` reads the backend’s Region from `baseUrl`. When the host doesn’t show it, such as on a custom domain, pass `region` or set `AWS_REGION`. A call that can’t be signed returns an error with the code `signing_failed`.

The browser client has no working admin methods. Every method under `backend.auth.admin`, such as `createUser`, `listUsers`, `banUser` and `removeUser`, fails with `AuthPluginNotEnabledError`. Call the admin methods on this page from server code, with `createServerAdmin`. For more information about roles, see [API keys and roles](backend-keys.md).

## Create users
<a name="backend-users-create"></a>

To create a user, run `user create`. It reads the password from a hidden prompt or from standard input, never from a flag.

```bash
# --name is optional and defaults to the part of the address before the @
printf %s "$TEST_PASSWORD" | aws amplifygen3 local backend user create --email test-owner-1@example.com --email-verified
```

```typescript
await admin.createUser({ email, password, name })
```

 `user create` and `createUser` send no verification email. They apply your `minPasswordLength` and `maxPasswordLength` settings, and refuse an address that already has an account.

### Invite users
<a name="backend-users-invite"></a>

To make your app invite-only, follow these steps:

1. Set `disableSignUp` in `emailAndPassword`, and in `magicLink` and `emailOtp` if you turned them on. A social or generic OAuth provider still creates an account at a user’s first sign-in. For the setting, see [Email and password](backend-auth-password.md).

1. Create each person’s account with `--email-verified` and a random password that nobody sees.

1. Ask each person to request a password reset from your app’s forgot-password page. The backend emails them a "Reset your password" link, and they choose their own password on your reset page.

```bash
printf %s "$(openssl rand -base64 24)" | aws amplifygen3 backend user create --env production --email rosie@example.com --email-verified
```

### Import users
<a name="backend-users-import"></a>

Passwords don’t move between sign-in services. The backend accepts a new password, never a password hash. Create each user without a password, and have each one set a password through password reset.

```typescript
await admin.createUser({ email, name, data: { emailVerified: true } })
```

## Find users
<a name="backend-users-find"></a>

To list users, run `user list`. It sorts users by email. To filter the list, add `--email` or `--name` with text that the user’s email or name contains. `user get` shows one user, by `--id` or `--email`.

```bash
aws amplifygen3 backend user list --env production --email @example.com
```

```typescript
const { data } = await admin.listUsers({ limit: 50, offset: 0 })  // data: { users, total }
await admin.getUser({ id })
```

## Update users
<a name="backend-users-update"></a>

To change a user, run `user edit` with `--id` or `--email`, and any of `--name`, `--new-email`, `--email-verified` and `--password`. A new email is unverified unless you add `--email-verified`.

```bash
aws amplifygen3 backend user edit --env production --email rosie@example.com --name "Rosie Q"
```

```typescript
await admin.updateUser({ userId, data: { name } })
await admin.updateUser({ userId, data: { email, emailVerified: true } })  // The backend marks a new email unverified unless you pass emailVerified: true.
await admin.setUserPassword({ userId, newPassword })
```

Setting a new password doesn’t sign the user out of their current sessions. If the old one was stolen, revoke their sessions too, or call `banUser`. A ban refuses every sign-in and signs the user out until you call `unbanUser`.

A signed-in user changes their own name with `backend.auth.updateUser({ name })` from the client SDK. To let users change their email address or delete their account, turn on `user.changeEmail` or `user.deleteUser` in [Auth configuration](backend-auth-config.md). The backend sends a confirmation email for each change.

To change a signed-in user’s email address, call `auth.changeEmail({ newEmail, callbackURL })`. `callbackURL` is optional. The backend sends the confirmation to the user’s current address. The current address must be verified, or the call fails with 403 `EMAIL_NOT_VERIFIED`. For a verified user, opening that link sends a second link to the new address. The new-address link signs the user in when it’s opened in a browser with no session.

You can’t delete the signed-in user’s account with the client SDK. With `user.deleteUser.enabled`, your app can call `POST /auth/delete-user` itself, and the backend emails a confirmation link.

## Revoke sessions and ban users
<a name="backend-users-revoke"></a>

```typescript
await admin.revokeUserSessions({ userId })  // signs the user out everywhere
await admin.banUser({ userId, reason })     // refuses every sign-in and signs the user out
await admin.unbanUser({ userId })
```

A banned user’s sign-in fails with 403 `BANNED_USER` until you call `unbanUser`.

## Delete users
<a name="backend-users-delete"></a>

To delete a user, run `user delete` with `--id` or `--email`, or call `admin.deleteUser({ userId })`. Unless you add `--yes` (`-y`), the command asks you to confirm. With `CI=true`, it fails instead.

```bash
aws amplifygen3 local backend user delete --email test-owner-1@example.com --yes
```

**Warning**  
 **a delete can’t be undone.** A delete removes the user, their sessions and their sign-in accounts. What happens to the user’s rows in your tables depends on the column that holds the user’s `id`:  
 **No foreign key** – The rows stay.
 ** `references auth."user"(id) on delete cascade` ** – The delete removes the rows.
 ** `on delete set null` ** – The delete clears the column.
 **A plain `references auth."user"(id)` ** – The delete fails with 409 `USER_HAS_DEPENDENT_ROWS`, naming your table, while the user still has rows.

## The users table
<a name="backend-users-table"></a>

 `aws amplifygen3 db query` and `aws amplifygen3 local db query` can read only the `id`, `name`, `email`, `"emailVerified"` and `"createdAt"` columns of `auth."user"`. Name the columns, because `select *` fails with a permission error. The data API doesn’t serve the table.

A user’s `id` is `text`. To tie your rows to a user, give the table a `text` owner column that references it:

```sql
owner_id text not null default auth.uid() references auth."user"(id) on delete cascade
```

### Create a profile at first sign-in
<a name="backend-users-profile"></a>

A migration can’t add a trigger on `auth."user"`: it fails with `permission denied for table user`. To create a profile row for each user, have your app write it once, at the user’s first sign-in, under an insert policy `with check (id = (select auth.uid()))`. For policies, see [Secure your data](backend-rls.md).

```typescript
await backend.from('profiles').upsert({ id: session.user.id, name }, { ignoreDuplicates: true })
```

For every command and flag, see [CLI reference](aws-cli-support-chapter.md).