satansdeer / react-firebase-auth

React Firebase Authentication Example
https://maksimivanov.com/posts
MIT License
331 stars 181 forks source link

How to add signOut? #29

Open Nithur-M opened 3 years ago

Nithur-M commented 3 years ago

Hi, can you let me know how to sign user out after creating user with email?. I want the user to verify his email before using the app. I used auth().signOut() but it isn't working.

UrvaSuthar commented 1 year ago
import { getAuth, signOut } from 'firebase/auth';
import { initializeApp } from 'firebase/app';

// Initialize Firebase app with configuration
const app = initializeApp({
  // Add your Firebase configuration here
  apiKey: 'your-api-key',
  authDomain: 'your-auth-domain',
  projectId: 'your-project-id',
  // ...
});

// Create the authentication instance
const auth = getAuth(app);

// Function to handle sign out
const handleSignOut = () => {
  signOut(auth)
    .then(() => {
      // Sign out successful
      console.log('User signed out successfully.');
    })
    .catch((error) => {
      // An error occurred during sign out
      console.error('Error signing out:', error);
    });
};

// Render the sign out button in your React component
const MyComponent = () => {
  return (
    <button onClick={handleSignOut}>Sign Out</button>
  );
};

export default MyComponent;

This code snippet demonstrates importing getAuth and signOut from firebase/auth. It initializes the Firebase app with your configuration, creates an authentication instance, and defines a handleSignOut function that calls signOut(auth) to sign out the user.

You can use the handleSignOut function as the onClick event handler for your sign-out button. In this example, the button is rendered within a React component called MyComponent.