# Migrate from Amazon Cognito

> Move Amazon Cognito user pool profiles, groups and federated identities into Pontive with pontive migrate. Cognito exports no password hashes, so password users set a new one at their first sign-in. Then replace Amplify with pontkit.

Source: https://www.pontive.com/docs/guides/migrate/cognito

The steps below use the `pontive migrate` CLI. Read [Migrate to Pontive](/docs/guides/migrate) first
for how runs, dry runs and matching work.

## 1. What moves and what does not [#1-what-moves-and-what-does-not]

| Moves | Does not move |
| --- | --- |
| Users from `ListUsers`: email and `email_verified`, `phone_number` and its verified flag, `preferred_username`, `name`, `given_name`, `family_name`, `picture`, `locale`, the creation date; disabled users as deactivated | **Password hashes.** No Cognito API returns them, so every password user uses **Forgot password** at their first Pontive sign-in |
| Custom attributes (`custom:…`, without the prefix) as `private_metadata` | **TOTP and SMS factors**: no secrets are exportable, so those users enroll again |
| Groups, as roles on the instance mapped with `--role-map` (step 4), and as `metadata.cognito_groups` on each user | Groups as organizations: Cognito groups are pool-wide, so write those yourself (step 4) |
| Google, Sign in with Apple and OIDC identities whose `userId` is the provider's subject | SAML, Facebook and Login with Amazon identities, and identities linked on another attribute: those users link by verified email |
| The user's `sub`, as legacy subject and `external_id` | Users without an email |

Since every password user resets, tell them before the cutover, from your own system: Pontive sends
nothing to imported users.

## 2. Create the migration source [#2-create-the-migration-source]

```sh
export PONTIVE_SECRET_KEY=pontive_sk_test_us_...   # an admin key of your development instance

npx pontive@latest migrate source create --name "Cognito" --type cognito   # prints msrc_...
```

## 3. Export, then dry run [#3-export-then-dry-run]

The exporter uses the AWS SDK's default credentials (environment, profile or SSO) and needs
`cognito-idp:ListUsers`, `ListGroups` and `ListUsersInGroup` on the pool. The region is
`AWS_REGION`, else the pool id's prefix.

```sh
export AWS_PROFILE=prod

npx -p pontive@latest -p @aws-sdk/client-cognito-identity-provider \
  pontive migrate export cognito --user-pool-id us-east-1_AbC123     # writes pontive-users.ndjson
npx pontive@latest migrate import pontive-users.ndjson --source msrc_...
```

The AWS SDK is not installed with `pontive`, hence `-p`. The report's *password users without a
hash* is every user who is not federated-only.

## 4. Map roles and connections [#4-map-roles-and-connections]

**Identities.** Map them to the Pontive app's auth connections. Import Apple identities only when
your Pontive Apple connection uses the same Apple developer team, and `oidc` ones only for the same
issuer:

```sh
npx pontive@latest migrate import pontive-users.ndjson --source msrc_... \
  --identity-connection google=app_...:google
```

**Groups** become roles on the instance. The export writes each user's groups as their root roles
(`root_roles`), and the import adds every imported user to the instance with them, once the user
exists. Create each role on the `root` type first
([`POST /v1/authz/resource-types/root/roles`](/docs/api-reference/management/roles)), then map the
group names to the role keys:

```sh
npx pontive@latest migrate import pontive-users.ndjson --source msrc_... \
  --identity-connection google=app_...:google \
  --role-map editors=editor,admins=admin
```

The dry run checks that each mapped role exists on `root` (`role_not_found`). With a map, a user in
a group the map does not name gets none of their roles and is reported as `role_not_mapped`, though
the user is still imported; without a map, group names are used as role keys. `owner` is never
granted this way, and disabled users get no roles. A token with no organization then carries those
roles in `roles`, where Cognito's carried `cognito:groups`. The groups also stay on each user as
`metadata.cognito_groups`.

**Customer groups** in a `b2b` project, which stood for a tenant rather than a permission, become
organizations instead. Remove those group names from each user's `root_roles` in the file, or the map
reports those users `role_not_mapped`, and append an `organization` line per group before importing,
members named by the Cognito `sub`, at least one of them `owner`:

```json
{"kind":"organization","external_id":"acme","name":"Acme Corp","members":[{"legacy_subject":"6f1c…","roles":["owner"]},{"legacy_subject":"92ab…","roles":["admin"]}]}
```

## 5. Apply [#5-apply]

```sh
npx pontive@latest migrate import pontive-users.ndjson --source msrc_... \
  --identity-connection google=app_...:google \
  --role-map editors=editor,admins=admin \
  --apply --delete-after
```

The report counts the root members added beside the users.

Failed rows: `npx pontive@latest migrate records uimp_... --outcome failed`.

## 6. Swap the SDK [#6-swap-the-sdk]

Follow the [React quickstart](/docs/quickstart/react) or the [Next.js quickstart](/docs/quickstart/nextjs),
then replace Amplify:

```sh
npm uninstall aws-amplify @aws-amplify/ui-react
npm install @pontive/pontkit-react
```

| Amplify and Cognito | pontkit |
| --- | --- |
| `Amplify.configure(…)` and `<Authenticator>` | `<Provider domain appId signinUrl signupUrl>` with `<Signin />` and `<Signup />` pages |
| `signIn`, `signUp`, `confirmSignUp`, `resetPassword` | the flows inside `<Signin />` and `<Signup />`, including forgot password |
| `getCurrentUser()` | `Auth.getAuthenticatedUser()`, `Auth` from `@pontive/oidc-client` |
| `fetchAuthSession()`, `tokens.accessToken` | `Auth.getCurrentSession()?.access_token` |
| `signOut()` | `Auth.signOut()` |
| `aws-jwt-verify`'s `CognitoJwtVerifier` | `rs.requireBearer()` from `@pontive/pontkit-server/resource-server` ([Protect an API](/docs/guides/protect-an-api)) |
| The `cognito:groups` claim | the `roles` and `permissions` claims |
| The Hosted UI on `*.amazoncognito.com` | your instance's auth domain: register the app's redirect URIs and allowed origins there |

## 7. Cut over [#7-cut-over]

Follow the [cutover checklist](/docs/guides/migrate#cut-over). For Cognito in particular:

- Export again just before switching and import into the same run with `--run uimp_...`.
- If your data keys users by Cognito `sub`, set the app's claims template to
  `{ "legacy_user_id": "{{ user.external_id }}" }`.
- Turn off the user pool's sign-ups and messages once the app is on Pontive.

## 8. Revert [#8-revert]

```sh
npx pontive@latest migrate revert uimp_...          # dry run
npx pontive@latest migrate revert uimp_... --apply
```

Users who have signed in to Pontive since are kept and listed. See
[Revert](/docs/guides/migrate#revert).
