Skip to content

Commit

Permalink
Improve sample & readme
Browse files Browse the repository at this point in the history
  • Loading branch information
WietseWind committed Nov 9, 2022
1 parent 727c42a commit f4526b1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,52 @@ NPM:

## Sample:

https://github.com/XRPL-Labs/XummPkce/blob/main/sample/index.html
#### Promise based sample

```javascript
const xumm = new XummPkce("uuid-uuid-uuid-uuid");

const xummSignInHandler = (state) => {
const { sdk, me } = state;
console.log("state", me);
// Also: sdk » xumm-sdk (npm)
};
// To pick up on mobile client redirects:
xumm.on("retrieved", async () => {
console.log("Retrieved: from localStorage or mobile browser redirect");
xummSignInHandler(await xumm.state());
});

// E.g. when clicking a button:
document.getElementById("somebutton").onclick = () => {
xumm.authorize().then((session) => {
xummSignInHandler(session);
});
};
```

#### Event based sample

```javascript
const xumm = new XummPkce("uuid-uuid-uuid-uuid");

xumm.on("error", (error) => {
console.log("error", error);
});

xumm.on("success", async () => {
const state = await xumm.state();
console.log("success:", state.me);
// Also: state.sdk » xumm-sdk (npm)
});

xumm.on("retrieved", async () => {
console.log("Retrieved: from localStorage or mobile browser redirect");
const state = await xumm.state();
console.log("retrieved:", state.me);
// Also: state.sdk » xumm-sdk (npm)
});
```

### CDN (browser):

Expand Down
24 changes: 19 additions & 5 deletions sample/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ <h2>Hello, world!</h2>
var sdk = null

auth.on('error', error => {
alert(
'error: ' + error
?.message)
console.log('error', error)
})

auth.on('success', async () => {
alert('success: ' + JSON.stringify((await auth.state()).me))
// alert('success: ' + JSON.stringify((await auth.state()).me))
console.log('success')
auth
.state()
.then(r => {
if (r.me) {
console.log('success, me', JSON.stringify(r.me))
}
})
})

auth.on('retrieved', async () => {
Expand All @@ -84,7 +90,15 @@ <h2>Hello, world!</h2>

go()

alert('retrieved: ' + JSON.stringify((await auth.state()).me))
// alert('retrieved: ' + JSON.stringify((await auth.state()).me))
console.log('retrieved')
auth
.state()
.then(r => {
if (r.me) {
console.log('retrieved, me', JSON.stringify(r.me))
}
})
})

/**
Expand Down

0 comments on commit f4526b1

Please sign in to comment.