Get eth address of user. React Moralis

Try to authenticate the user to database and get the ethaddress to database but it’s comming empty screenshot will help to find what is happeningIMG_20210915_193041_355 IMG_20210915_193035_099

Can you show the full POST url where it says bad request? And maybe the parameters and what is shows in dashboard logs.

We just want ethaddress of current user we tried putting console log and print user but it is showing null

I don’t understand that from what you posted, did you try: web3.currentProvider.selectedAddress , window.ethereum.selectedAddress? Or is something totally different what you ask?

Please format your code :raised_hands:

import React from "react";
import { useMoralis } from "react-moralis";

export default function Login() {
  const [username, setUsername] = React.useState<string>("");
  const [password, setPassword] = React.useState<string>("");

  
  const {
    signup,
    login,
    logout,
    isAuthenticating,
    isAuthenticated,
    user,
    setUserData,
    userError,
    isUserUpdating,
    authenticate,
    Moralis
  } = useMoralis();

  const currentuser = Moralis.User.current();
  console.log('user', currentuser);

  const User = () => {
    if (!isAuthenticated) {
      return (
        <div>
          <button
            onClick={() =>
              authenticate({ signingMessage: "Welcome"})
              
            }
          >
            Authenticate
          </button>
        </div>
      );
    }

    return (
      <div>
        <h1>Hi {user.get("username")}</h1>
      </div>
    );
  };

  return (
    <section className="mt-40">
      <h1>Welcome to the App</h1>

      <p>You can try out logging in below!</p>

      <div>
        <input
          type="text"
          value={username}
          onChange={(e: any) => setUsername(e.target.value)}
          placeholder="Username"
        />
      </div>
      <div>
        <input
          type="password"
          value={password}
          onChange={(e: any) => setPassword(e.target.value)}
          placeholder="Password"
        />
      </div>
      <div>
        <button onClick={() => login(username, password)}>Login</button>
      </div>
      <div>
        <button onClick={() => signup(username, password)}>Sign up</button>
      </div>
      <div>
        <button onClick={() => logout()} disabled={isAuthenticating}>
          Logout
        </button>
      </div>
      <button
        onClick={() =>
          setUserData({
            username: username,
            password: password,
          })
        }
        disabled={isUserUpdating}
      >
        Set user data
      </button>

      <User />

      {userError && <p>Error: {userError.message}</p>}

      <pre>User(in JSON): {JSON.stringify(user)}</pre>
    </section>
  );
}
1 Like

authenticate({ signing: "Moralis Authentication" })

We have a documentation for react-moralis

I want current users addres I am not getting that

I’ll send you correct example soon. Because you have a lot functions from not react SDK. like this:

const currentuser = Moralis.User.current();

You can get it from

const { user } = useMoralis();

You can get username using user.attributes.username

import { useMoralis } from "react-moralis";

export default function Login() {
const { user } = useMoralis();
const [username, setUsername] = React.useState<string>("");

useEffect(()=> {
 if (user) {
  setUsername(user.attributes.username)
  }
 }, [user])

}

2 Likes

Thanks a lot for help