Skip to main content
Since the user’s keypair is encrypted with a pincode and stored in their browser, we will occasionally need to prompt them for that pincode to sign a transaction or otherwise prove that they should be permitted to perform some action or view some data.

User Experience

The user should be informed about any actions that may take place, especially when funds are on the line. To ensure this, we will overtly request their confirmation via pincode before anything is done. The application has no way of knowing a user’s pincode, so it can’t decrypt their keypair without their confirmation. The modal window we’ve implemented facilitates this confirmation flow whenever we need it. confirmation modal

Code implementation

Our modal function uses the svelte-simple-modal package to give us a versatile starting point. If you need to, install it now.

Wrapping the rest of our app in the modal

On the Svelte side, this modal component will be a “wrapper” around the rest of our application, which allows us to trigger the modal from anywhere we need, and it should behave similarly no matter what.
/src/routes/+layout.svelte
Source: https://github.com/stellar/basic-payment-app/blob/main/src/routes/+layout.svelte

Creating a reusable modal Svelte component

To avoid reinventing the wheel every time we need a modal, we will create a reusable component that can accomodate most of our needs. Then, when we need the confirmation modal, we can pass an object of props to customize the modal’s behavior.
In our *.svelte component files, we will not dive into the HTML markup outside of the <script> tags. The Svelte syntax used in HTML is primarily used for iterating and is quite understandable to read.
The basic parts of this component look like this:
/src/lib/components/ConfirmationModal.svelte
Source: https://github.com/stellar/basic-payment-app/blob/main/src/lib/components/ConfirmationModal.svelte

Trigger the modal component at signup

We can now use this modal component whenever we need to confirm something from the user. For example, here is how the modal is triggered when someone signs up.
/src/routes/signup/+page.svelte
Source: https://github.com/stellar/basic-payment-app/blob/main/src/routes/signup/+page.svelte

Customizing confirmation and rejection behavior

Now, as these components have been written so far, they don’t actually do anything when the user inputs their pincode or clicks on a button. Let’s change that! Since the confirmation behavior must vary depending on the circumstances (for example, different actions for signup, transaction submission, etc.), we need a way to pass that as a prop when we open the modal window. First, in our modal component, we declare a dummy function to act as a prop, as well as an “internal” function that will call the prop function during the course of execution.
/src/lib/components/ConfirmationModal.svelte
Source: https://github.com/stellar/basic-payment-app/blob/main/src/lib/components/ConfirmationModal.svelte Now that our modal component is setup to make use of a prop function for confirmation and rejection, we can declare what those functions should do inside the page that spawns the modal.
/src/routes/signup/+page.svelte
Source: https://github.com/stellar/basic-payment-app/blob/main/src/routes/signup/+page.svelte As you can see, we didn’t actually need a customized onReject function, so we didn’t pass one. No harm, no foul!