constructor( private afs: AngularFirestore, private authService: AuthService ) { this.authService.getAuthState() .subscribe((user) => { if (user) { // User is signed in. this.userId = user.uid; this.username = user.email; } else { this.userId = undefined; } }); }
sendMessage() { console.log("send message triggered"); const { message } = this.state; const { currentUser } = firebaseApp.auth(); messageRef.push({ message: message, author: currentUser.email }); }
postComment(dishId: string, comment: any): Promise<any> { if (this.currentUser) { return this.afs.collection('dishes').doc(dishId).collection('comments') .add({ author: { '_id': this.currentUser.uid, 'firstname' : this.currentUser.displayName ? this.currentUser.displayName : this.currentUser.email }, rating: comment.rating, comment: comment.comment, createdAt: firebase.firestore.FieldValue.serverTimestamp(), updatedAt: firebase.firestore.FieldValue.serverTimestamp() }); } else { return Promise.reject(new Error('No User Logged In!')); } }
firebase.auth().onAuthStateChanged((user) => { if (user) { // console.log(user) const name = user.displayName ? user.displayName : user.email; store.dispatch(login(user.uid, name)); store.dispatch(setStartState()); renderApp(); if (history.location.pathname === '/') { history.push('/join'); } } else { store.dispatch(logout()); store.dispatch(clearState); renderApp(); history.push('/'); } });
listenForItems(chatRef) { chatRef.on('value', (snap) => { // get children as an array var items = []; snap.forEach((child) => { var avatar = 'https://www.gravatar.com/avatar/' + ( child.val().uid == this.user.uid? md5(this.user.email) : md5(this.friend.email)) var name = child.val().uid == this.user.uid? this.user.name: this.friend.name items.push({ _id: child.val().createdAt, text: child.val().text, createdAt: new Date(child.val().createdAt), user: { _id: child.val().uid, avatar: avatar } }); }); this.setState({ loading: false, messages: items }) }); }
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 }); }); }
value={{ photoURL: user.photoURL || MAGIC_URL, email: user.email }}
React.createClass({ getInitialState: function(){ return { user: null }; }, componentWillMount: function() { var user = Firebase.auth().currentUser; this.setState({user: user.email }); }, render: function() { if(!this.state.user){ return ( <View> <Text style={styles.container}>Loading...</Text> </View> ) } var username = this.state.user; return ( <View style={styles.container}> <Text>Welcome back {username}!</Text> </View> ) } })