# Update Recovery Method

Once a wallet has been created, users have the ability to upgrade from automatic recovery to a user-owned recovery method, or to switch between different user-owned recovery methods.

## Upgrading a user's recovery method

To update the recovery method, call `setRecoveryMethod` with both the previous recovery params and new recovery params:

```tsx
await openfort.embeddedWallet.setRecoveryMethod(previousRecovery, newRecovery);
```

To update the recovery method, you need to provide either the user's current password or an encryption session.

:::tip
Users can **reset their password** using this method as well.
:::

When calling `setRecoveryMethod`, the recovery method is updated to the new method provided. The user won't need to reconstruct their private key immediately after.

As an example, you might add `setRecoveryMethod` as an event handler for a set recovery button in your app:

:::code-group

```tsx [UpdateRecoveryButton.tsx]
import openfort from './openfortConfig';
import {RecoveryMethod} from '@openfort/openfort-js';
import getEncryptionSession from "./encryptionSession"

// This example assumes you have already checked that Openfort 'embeddedState' is
// `ready` and the user is `authenticated`
function AddOrUpdateRecoveryButton() {
  const handleUpdateRecovery = async () => {
    const password = (
        document.querySelector(
          `input[name="password"]`
        ) as HTMLInputElement
      ).value;

    const encryptionSession = await getEncryptionSession();

    // Previous recovery method (e.g., automatic)
    const previousRecovery = {
      recoveryMethod: RecoveryMethod.AUTOMATIC,
      encryptionSession,
    };

    // New recovery method (e.g., password)
    const newRecovery = {
      recoveryMethod: RecoveryMethod.PASSWORD,
      password,
    };

    await openfort.embeddedWallet.setRecoveryMethod(previousRecovery, newRecovery);
  };

  return (
    <div>
      <input
        name={'password'}
        placeholder="password recovery"
      />
      <button onClick={handleUpdateRecovery}>
        {'Add recovery to your wallet'}
      </button>
    </div>
  );
}
```

```ts [encryptionSession.ts]
const getEncryptionSession = async (): Promise<string> => {
    try {
        const response = await fetch('https://your-api-endpoint.com/api/protected-create-encryption-session', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({})
        });
        const data = await response.json();
        return data.session;
    } catch (error) {
        throw new Error('Failed to create encryption session');
    }
};
export default getEncryptionSession;
```

```ts [openfortConfig.ts]
import { Openfort } from '@openfort/openfort-js';

const openfort = new Openfort({
  baseConfiguration: {
    publishableKey: "YOUR_OPENFORT_PUBLISHABLE_KEY",
  },
  shieldConfiguration: {
    shieldPublishableKey: "YOUR_SHIELD_PUBLISHABLE_KEY",
  },
});

export default openfort;
```

:::
