|
8 | 8 | </form>
|
9 | 9 | </div>
|
10 | 10 | {% endblock content %}
|
| 11 | +{% block javascript %} |
| 12 | + {{ block.super }} |
| 13 | + <script> |
| 14 | + const signup = '{% url 'web3auth_signup_api' %}'; |
| 15 | + const CHAIN_ID = '038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca'; |
| 16 | + const CHAIN_PROTOCOL = 'http'; |
| 17 | + const CHAIN_HOST = '193.93.219.219'; |
| 18 | + const CHAIN_PORT = 8888; |
| 19 | + |
| 20 | + const NETWORK = { |
| 21 | + protocol: CHAIN_PROTOCOL, |
| 22 | + blockchain: 'eos', |
| 23 | + host: CHAIN_HOST, |
| 24 | + port: CHAIN_PORT, |
| 25 | + chainId: CHAIN_ID |
| 26 | + }; |
| 27 | + |
| 28 | + function signupWithData(username, email, signup_url, onSignupRequestError, onSignupSuccess, onSignupFail) { |
| 29 | + var request = new XMLHttpRequest(); |
| 30 | + request.open('POST', signup_url, true); |
| 31 | + request.onload = function () { |
| 32 | + if (request.status >= 200 && request.status < 400) { |
| 33 | + // Success! |
| 34 | + var resp = JSON.parse(request.responseText); |
| 35 | + if (resp.success) { |
| 36 | + if (typeof onSignupSuccess === 'function') { |
| 37 | + onSignupSuccess(resp); |
| 38 | + } |
| 39 | + } else { |
| 40 | + if (typeof onSignupFail === 'function') { |
| 41 | + onSignupFail(resp); |
| 42 | + } |
| 43 | + } |
| 44 | + } else { |
| 45 | + // We reached our target server, but it returned an error |
| 46 | + console.log("Signup failed - request status " + request.status); |
| 47 | + if (typeof onSignupRequestError === 'function') { |
| 48 | + onSignupRequestError(request); |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + request.onerror = function () { |
| 54 | + console.log("Signup failed - there was an error"); |
| 55 | + if (typeof onSignupRequestError === 'function') { |
| 56 | + onSignupRequestError(request); |
| 57 | + } |
| 58 | + // There was a connection error of some sort |
| 59 | + }; |
| 60 | + request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); |
| 61 | + request.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); |
| 62 | + var formData = 'username=' + username + '&email=' + email; |
| 63 | + request.send(formData); |
| 64 | + } |
| 65 | + |
| 66 | + async function requestIdentity(onIdentityReject) { |
| 67 | + await scatter.suggestNetwork(NETWORK); |
| 68 | + |
| 69 | + let required_fields = { |
| 70 | + personal: ['email'], |
| 71 | + accounts: [NETWORK] |
| 72 | + }; |
| 73 | + |
| 74 | + scatter.getIdentity(required_fields).then((identity) => { |
| 75 | + var account = identity.accounts.find(function (account) { |
| 76 | + return account.blockchain === 'eos' |
| 77 | + }); |
| 78 | + |
| 79 | + signupWithData(account.name, scatter.identity.personal.email, signup, console.log, console.log, console.log) |
| 80 | + |
| 81 | + }).catch(error => { |
| 82 | + console.log("Identity or Network was rejected"); |
| 83 | + if (typeof onIdentityReject === 'function') { |
| 84 | + onIdentityReject(error); |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + document.addEventListener('scatterLoaded', scatterExtension => { |
| 90 | + console.log("Scatter installed!"); |
| 91 | + if (scatter.identity) { |
| 92 | + // login the user with api |
| 93 | + console.log("Identity found"); |
| 94 | + console.log(scatter.identity); |
| 95 | + console.log("Welcome, " + scatter.identity.name + '<br>Your public key: ' + scatter.identity.publicKey); |
| 96 | + } else { |
| 97 | + requestIdentity() |
| 98 | + } |
| 99 | + }); |
| 100 | + </script> |
| 101 | +{% endblock javascript %} |
0 commit comments