Created
July 27, 2021 21:14
-
-
Save freddiemixell/192c9373f87f9b1a9496192b3ad30a2e to your computer and use it in GitHub Desktop.
Firebase Login Hooks
This file contains hidden or 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
| /** | |
| * Sign In | |
| * | |
| * @returns {Promise} with user object. | |
| **/ | |
| export default function signInWithEmailAndPassword( email, password ) { | |
| if ( ! email || ! password ) { | |
| alert( `Enter your ${ ! email ? 'email' : 'password' } and try again.` ); | |
| return; | |
| } | |
| let firebase = useFirebase(); | |
| return firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL) | |
| .then(async () => { | |
| // Existing and future Auth states are now persisted in the current | |
| // session only. Closing the window would clear any existing state even | |
| // if a user forgets to sign out. | |
| // ... | |
| // New sign-in will be persisted with session persistence. | |
| try { | |
| const { user } = await firebase.auth().signInWithEmailAndPassword(email, password); | |
| console.log( user ); | |
| return user; | |
| } catch (error) { | |
| var errorCode = error.code; | |
| var errorMessage = error.message; | |
| alert(errorMessage); | |
| } | |
| }) | |
| .catch((error) => { | |
| // Handle Errors here. | |
| var errorCode = error.code; | |
| var errorMessage = error.message; | |
| console.log( `Firebase Error: ${errorCode}` ); | |
| console.log( errorMessage ); | |
| }); | |
| } |
This file contains hidden or 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
| import firebase from 'firebase/app'; | |
| import 'firebase/auth'; | |
| // import 'firebase/firestore'; You'll need these if you use firestore or the file storage. | |
| // import 'firebase/storage'; | |
| const firebaseConfig = { | |
| apiKey: "YOUR_ID", | |
| authDomain: "YOUR_ID.firebaseapp.com", | |
| projectId: "YOUR_ID", | |
| storageBucket: "YOUR_ID.appspot.com", | |
| messagingSenderId: "YOUR_ID", | |
| appId: "YOUR_ID" | |
| }; | |
| export default function useFirebase() { | |
| if ( ! firebase.apps.length ) { | |
| firebase.initializeApp( firebaseConfig ); | |
| } | |
| return firebase; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment