getInitialView() { firebase.auth().onAuthStateChanged((user) => { let initialView = user ? "Home" : "Login"; this.setState({ userLoaded: true, initialView: initialView }) }); }
const fetchEmployees = () => { const {currentUser} = firebase.auth(); return (dispatch) => { firebase.database().ref(`/users/${currentUser.uid}/employees`) .on('value', (snapshot) => { dispatch({type: FETCH_EMPLOYEE_SUCCESS, payload: snapshot.val()}) }); //TODO catch error } }
const logout = () => async dispatch => { try { const firebase = require('firebase'); // eslint-disable-line global-require await firebase.auth().signOut(); console.log('User Logged Out!'); dispatch({ type: LOGOUT_SUCCESS }); } catch (err) { console.log('err:', err); } }
onLoginPress() { this.setState({ error: '', loading: true }); const { email, password } = this.state; firebase.auth().signInWithEmailAndPassword(email, password) .then(() => { this.setState({ error: '', loading: false }); this.props.navigation.navigate('Home'); }) .catch(() => { this.setState({ error: 'Authentication failed', loading: false }); }); }
firebase .auth() .createUserWithEmailAndPassword(this.state.email, this.state.password) .then((user) => { this.setState({ isLoading: false }); console.log('User', user, 'registered!'); this.props.navigation.navigate('Search'); }) .catch((error) => { this.setState({ isLoading: false, error: error.message }); });
const signIn = (email, password) => { return firebase.auth().signInWithEmailAndPassword(email, password) .then((response) => { return setSessionFromApi(response); }, (error) => { setSession(); return { errorCode: error.code, errorMessage: error.message }; }); }
const AuthContextProvider = ({ children }) => { const [user, setUser] = useState(() => { const user = firebase.auth().currentUser; return user; }); useEffect(() => { firebase.auth().onAuthStateChanged(firebaseUser => { setUser(firebaseUser); }); }, []) return <AuthContext.Provider value={user}>{children}</AuthContext.Provider> }
componentDidMount(){ firebase.auth().onAuthStateChanged((authenticate)=>{ if (authenticate) { this,this.props.navigation.replace('Home') } else { this,this.props.navigation.replace('Signin') } }) }
firebase.auth() .createUserWithEmailAndPassword(email, password) .catch((error) => { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; this.setState({ errorMessage, loading: false }) });
/** * @return {Promise} */ signout() { return firebase.auth().signOut(); }
firebase.auth().signOut().then(() => { navigator.pop(); }, function (error) { // An error happened. });
export function attachAuthListener(handler) { return firebase.auth().onAuthStateChanged(user => { handler(user); }); }
static onAuthStateChanged( callbackSuccess, callbackError, ) { return firebase .auth() .onAuthStateChanged(callbackSuccess, callbackError); }