Hello Moralis world
First of all, let me say thank you for creating this platform, it makes life for a developer/enthausiast so much easier.
Hower I do have a couple issues in the code i used from Moralis. I am trying to activate 2 options
- log in with Phantom Wallet
- show Phantom Wallet Address after login.
Issues:
-
I am able to login with the Phantom Wallet but it doesnât work 100% bulletproof. It seems like I am able to login once without issues and then either I keep logged in, even after refresh or just nothing happens when clicking on Connect Wallet.
-
When I try to show the adddress via a code I found on Moralis, I get âundefinedâ on my screen
Here is my code:
HTML:
button id=âbtn-loginâ class=âsc-button header-slider style style-1 wallet fl-button pri-1â style=âwidth: 80%; height: 50%; display: block; margin-left: 7%; margin-top: 5%;â>Connect Wallet
button id=âbtn-logoutâ class=âsc-button header-slider style style-1 wallet fl-button pri-1â style=âwidth: 80%; height: 50%; display: block; margin-left: 7%; margin-top: 5%;â>Disconnect Wallet
JAVASCRIPT:
*<!-- Moralis SDK code -->*
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
script>
*// connect to Moralis server*
const serverUrl = "https://ci6niblk6bbl.usemoralis.com:2053/server";
const appId = "y9ljzA94d2j44gorSyURoiNH3ZnkkaGPwSYZEyt3";
Moralis.start({ serverUrl, appId });
// add from here down
async function login() {
let user = Moralis.User.current();
if (!user) {
user = await Moralis.authenticate({ type: 'sol' }).then(function (user) {
console.log(user.get('solAddress'))
});
}
}
async function logOut() {
await Moralis.User.logOut();
console.log("logged out");
}
document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logOut;
/script>
script>
Moralis.User.currentAsync().then(function (user) {
var wallet = user.get('ethAddress');
document.getElementById('WalletAddressHere').innerHTML = wallet
});
/script>