2

Is there a way to logout of MetaMask from my own frontend, allowing the user to logout with a button rather going up to the Metamask extension and logging out through the traditional manner?

2 Answers 2

2

No, there is no way. This is by design, for security reasons (no change of address, login, logout, or any other sensitive operation that MetaMask requires the user to do himself).

2
  • A popup could have been implemented asking the user "do you want to disconnect?" in the same way every other action requires user confirmation. Jun 30, 2021 at 12:28
  • How is it safer to keep the account that was logged in once logged in forever? I was looking for this feature especially because I don't like my app skipping the "select wallet" screen just because I logged in once 2 months ago
    – Mouradif
    Feb 18, 2022 at 16:51
0

You can use Moralis for this. This is how you would be able to logout through a react or a next frontend:

import { useMoralis } from "react-moralis"

export default function Home() {
  const { isAuthenticated, logout } = useMoralis()
  // If you are logged in isAuthenticated is set to true
  return (
    <>
    {isAuthenticated ? 
       (<button onClick={logout}> </button>) 
     : (<div> <!-- Handle you login here --> </div>)}
    </>
  )
}

That's just a basic snippet for logout on click of a button. Read the npm docs for more info: link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.