Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
listenForItems(friendsRef) {
var user = firebase.auth().currentUser;
friendsRef.on("value", snap => {
// get children as an array
var items = [];
snap.forEach(child => {
if (child.val().email != user.email)
items.push({
name: child.val().name,
uid: child.val().uid,
email: child.val().email
});
});
this.setState({
dataSource: this.state.dataSource.cloneWithRows(items),
loading: false
})
.catch(error => {
const { code, message } = error;
alert(message);
// For details of error codes, see the docs
// The message contains the default Firebase string
// representation of the error
});
return;
}
const fbToken = await AsyncStorage.getItem(
"@loggedInUserID:facebookCredentialAccessToken"
);
if (id != null && id.length > 0 && fbToken != null && fbToken.length > 0) {
const credential = firebase.auth.FacebookAuthProvider.credential(fbToken);
firebase
.auth()
.signInWithCredential(credential)
.then(result => {
var user = result.user;
var userDict = {
id: user.uid,
fullname: user.displayName,
email: user.email,
profileURL: user.photoURL
};
this.props.navigation.dispatch({
type: "Login",
user: userDict
});
})
.catch(error => {
export const registerFirebaseAuthHandler = (onSignedIn) => {
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// signed in
if (user.isAnonymous) {
consolelog(`DEBUG firebase: signed in anonymously as: ${user.uid}`);
} else {
consolelog(`DEBUG firebase: signed in to an actual account as: ${user.uid}`);
}
onSignedIn(user);
} else {
consolelog('DEBUG firebase: no user, calling signInAnonymously()');
signInAnonymously();
}
});
};
onPress={async () => {
const data = await firebase
.auth()
.signInAnonymouslyAndRetrieveData();
console.warn(data);
}}
>
async onRegisterPress() {
this.setState({ errorMessage: null, loading: true });
const { email, password, name } = this.state;
console.log(email);
console.log(name);
console.log(password);
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then(() => {
this.setState({ loading: false });
})
.catch(error => {
var errorCode = error.code;
var errorMessage = error.message;
this.setState({ errorMessage, loading: false });
});
await AsyncStorage.setItem("email", email);
await AsyncStorage.setItem("name", name);
await AsyncStorage.setItem("password", password);
firebase.auth().onAuthStateChanged(user => {
static async signOut() {
return await firebase.auth().signOut();
}
() => {
firebase.auth().onAuthStateChanged(user => {
if (user) {
firebase.auth().signOut();
}
});
},
function(error) {
return new Promise((resolve, reject) => {
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then(user => {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
user = user.toJSON();
getUser(user.email)
.then(user => resolve(user))
.catch(error => reject(error));
} else {
reject();
}
});
})
.catch(error => reject(error));
});
async tryToLoginFirst() {
const email = await AsyncStorage.getItem("@loggedInUserID:key");
const password = await AsyncStorage.getItem("@loggedInUserID:password");
const id = await AsyncStorage.getItem("@loggedInUserID:id");
if (
id != null &&
id.length > 0 &&
password != null &&
password.length > 0
) {
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then(user => {
const { navigation } = this.props;
firebase
.firestore()
.collection("users")
.doc(id)
.get()
.then(function(doc) {
var dict = {
id: id,
email: email,
profileURL: doc.photoURL,
fullname: doc.displayName
};