Skip to content
This repository has been archived by the owner on Mar 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from funfair-tech/feature/lazy-loading+promise-…
Browse files Browse the repository at this point in the history
…login-updates

use the new lazy loading approach in samples
  • Loading branch information
joshstevens19 authored May 26, 2021
2 parents 7fbc929 + 7d9d5c0 commit 53d5a2f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 35 deletions.
34 changes: 24 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "vue-cli-service lint"
},
"dependencies": {
"@funfair-tech/wallet-sdk": "^3.0.3",
"@funfair-tech/wallet-sdk": "^3.4.0",
"@funfair-tech/wallet-vue": "^1.1.0",
"core-js": "^3.6.5",
"rxjs": "^6.6.3",
Expand Down
4 changes: 0 additions & 4 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
<title><%= htmlWebpackPlugin.options.title %></title>
<script
src="https://wallet.funfair.io/assets/sdk/fun-wallet-sdk.js?appId=0x1b084986077d1aedfa1d92318fdcc7d1621fbc92deb390269b94226fd79c0ce6"
type="text/JavaScript"
></script>
</head>
<body>
<noscript>
Expand Down
33 changes: 25 additions & 8 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<template>
<div id="app">
<WalletLeader :registerEventListeners="this.registerEventListeners" />
<div class="App">
<div class="App-container">
<img class="App-logo" src="./assets/logo.svg" />
Expand All @@ -22,7 +21,7 @@
</template>

<script>
import { WalletLeader, WalletFollower } from '@funfair-tech/wallet-vue';
import { WalletFollower } from '@funfair-tech/wallet-vue';
import { login, logout } from './services/wallet-service';
import { registerEventListeners } from './services/wallet-service';
import LoggedInActions from './components/Logged-In-Actions.vue';
Expand All @@ -31,11 +30,11 @@ import {
isAuthenticated$,
restoreAuthenticationTaskCompleted$,
} from './services/store';
import { FunWalletEmbed } from '@funfair-tech/wallet-sdk';
export default {
name: 'App',
components: {
WalletLeader,
LoggedInActions,
LoggedOutActions,
WalletFollower,
Expand All @@ -47,15 +46,33 @@ export default {
registerEventListeners: function() {
registerEventListeners();
},
login: function() {
login();
login: async function() {
await login();
},
logout: function() {
logout();
logout: async function() {
await logout();
},
},
created: function() {
created: async function() {
const _this = this;
// you call this method when you want to load the wallet
// this can be on a button click or page load up to how
// your dApp needs it to act
// it returns the fun wallet sdk but this
// is always exposed in `window.funwallet.sdk`
await FunWalletEmbed.load({
appId:
'0x1b084986077d1aedfa1d92318fdcc7d1621fbc92deb390269b94226fd79c0ce6',
// make sure its in a arrow expression
// functions so it can get context to `this`
// when executing your wallet event listener method
eventListenerCallback: () => {
this.registerEventListeners();
},
});
isAuthenticated$.subscribe((value) => {
_this.$data.isLoggedIn = value;
});
Expand Down
30 changes: 18 additions & 12 deletions src/services/wallet-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ import Web3 from 'web3';
import { isAuthenticated$, restoreAuthenticationTaskCompleted$ } from './store';

export function registerEventListeners() {
window.funwallet.sdk.on(
MessageListeners.authenticationCompleted,
(result) => {
if (result.origin === 'https://wallet.funfair.io') {
isAuthenticated$.next(true);
}
}
);

window.funwallet.sdk.on(
MessageListeners.restoreAuthenticationCompleted,
(result) => {
if (result.origin === 'https://wallet.funfair.io') {
restoreAuthenticationTaskCompleted$.next(true);

// if the user has been restored authentication then your all good
// to go again
if (result.data.isAuthenticated) {
// result.data.result holds `AuthenticationCompletedResponeData` in for you.
isAuthenticated$.next(true);
}
}
}
);
Expand Down Expand Up @@ -68,11 +66,19 @@ export function registerEventListeners() {
// register all the other events your interested in here...
}

export function login() {
window.funwallet.sdk.openWalletAuthenticationPopUp();
export async function login() {
try {
const result = await window.funwallet.sdk.auth.login();
console.log('Authentication result', result);

isAuthenticated$.next(true);
} catch (error) {
console.error('User did not sign in');
return;
}
}

export function logout() {
export async function logout() {
window.funwallet.sdk.auth.logout();
isAuthenticated$.next(false);
}
Expand Down

0 comments on commit 53d5a2f

Please sign in to comment.