Created
December 24, 2021 13:38
-
-
Save duartefdias/782838c9d449be035fa192f9641836d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async connectToMetamask() { | |
try { | |
// Connect to metamask and get user accounts | |
const accounts = await this.$store.getters['metamask/ethereum'].request({ method: 'eth_requestAccounts' }); | |
// Update vuex store | |
this.$store.commit('metamask/setMetamaskConnected', true) | |
this.$store.commit('metamask/setAccounts', accounts) | |
// Check if user is registered, if not, register them | |
const isRegistered = await this.checkIfUserRegistered(accounts[0]) | |
// Request nonce from backend | |
const responseNonce = await this.$axios.get('/users/' + accounts[0] + '/nonce') | |
const nonce = responseNonce.data | |
// Sign message | |
const signedMessage = await this.handleSignMessage(accounts[0], nonce) | |
// Send signature to backend | |
const responseSign = await this.$axios.post('/users/' + accounts[0] + '/signature', signedMessage) | |
// Set token in local vuex state store | |
this.$store.commit('metamask/setToken', responseSign.data.token) | |
// If successful, redirect to home | |
if (responseSign.status === 200) { | |
this.$router.push('/') | |
} | |
} catch (error) { | |
console.log(error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment