From fd226b454ed759c34e37ae867f9d49cf3c3f276f Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Sun, 27 Oct 2019 14:46:31 -0400 Subject: [PATCH 1/7] Delete users fully works now --- functions/handlers/users.js | 8 ++------ functions/index.js | 2 +- twistter-frontend/src/pages/Delete.js | 12 ++++++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/functions/handlers/users.js b/functions/handlers/users.js index 7e4b018..cc54149 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -151,18 +151,14 @@ exports.deleteUser = (req, res) => { firebase.auth().onAuthStateChanged(function(user) { currentUser = user; if (currentUser) { - /*db.collection("users").doc(`${currentUser.handle}`).delete() + db.collection("users").doc(`${req.user.handle}`).delete() .then(function() { res.status(200).send("Removed user from database."); return; }) .catch(function(err) { res.status(500).send("Failed to remove user from database.", err); - });*/ - - //let ref = db.collection('users'); - //let userDoc = ref.where('userId', '==', currentUser.uid).get(); - //userDoc.ref.delete(); + }); currentUser.delete() .then(function() { diff --git a/functions/index.js b/functions/index.js index 41a126c..45d181a 100644 --- a/functions/index.js +++ b/functions/index.js @@ -29,7 +29,7 @@ app.post("/signup", signup); app.post("/login", login); //Deletes user account -app.delete("/delete", deleteUser); +app.delete("/delete", fbAuth, deleteUser); app.get("/getUser/:handle", getUserDetails); diff --git a/twistter-frontend/src/pages/Delete.js b/twistter-frontend/src/pages/Delete.js index ab610b1..eafeecf 100644 --- a/twistter-frontend/src/pages/Delete.js +++ b/twistter-frontend/src/pages/Delete.js @@ -7,7 +7,8 @@ import Button from "@material-ui/core/Button"; import withStyles from "@material-ui/core/styles/withStyles"; // Redux stuff -import { logoutUser } from "../redux/actions/userActions"; +//import { logoutUser } from "../redux/actions/userActions"; +import { deleteUser } from "../redux/actions/userActions"; import { connect } from "react-redux"; const styles = { @@ -32,7 +33,8 @@ const styles = { export class Delete extends Component { componentDidMount() { - this.props.logoutUser(); + //this.props.logoutUser(); + this.props.deleteUser(); this.props.history.push('/'); } @@ -45,10 +47,12 @@ const mapStateToProps = (state) => ({ user: state.user }); -const mapActionsToProps = { logoutUser }; +//const mapActionsToProps = { logoutUser }; +const mapActionsToProps = { deleteUser }; Delete.propTypes = { - logoutUser: PropTypes.func.isRequired, + //logoutUser: PropTypes.func.isRequired, + deleteUser: PropTypes.func.isRequired, user: PropTypes.object.isRequired, classes: PropTypes.object.isRequired }; From 5fa4caf0a34c4cbe9e5e910c7e6d5f5ae3d6bb7b Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Sun, 27 Oct 2019 17:47:04 -0400 Subject: [PATCH 2/7] Log in with username fully works now --- functions/handlers/users.js | 60 +++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/functions/handlers/users.js b/functions/handlers/users.js index cc54149..87733bc 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -7,8 +7,6 @@ const { validateUpdateProfileInfo } = require("../util/validator"); const firebase = require("firebase"); firebase.initializeApp(config); -var handle2Email = new Map(); - exports.signup = (req, res) => { const newUser = { email: req.body.email, @@ -79,7 +77,6 @@ exports.signup = (req, res) => { createdAt: newUser.createdAt, userId }; - handle2Email.set(userCred.handle, userCred.email); return db.doc(`/users/${newUser.handle}`).set(userCred); }) .then(() => { @@ -97,7 +94,6 @@ exports.signup = (req, res) => { exports.login = (req, res) => { const user = { email: req.body.email, - handle: req.body.handle, password: req.body.password }; @@ -106,25 +102,62 @@ exports.login = (req, res) => { const emailRegEx = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; - // Email check + // Checks if email/username field is empty if (user.email.trim() === "") { errors.email = "Email must not be blank."; } - else if (!user.email.match(emailRegEx)) { - user.email = handle2Email.get(user.email); - } - // Password check + // Checks if password field is empty if (user.password.trim() === "") { errors.password = "Password must not be blank."; } - // Checking if any errors have been raised + // Checks if any of the above two errors were found if (Object.keys(errors).length > 0) { return res.status(400).json(errors); } - firebase + // Email/username field is username since it's not in email format + if (!user.email.match(emailRegEx)) { + var userDoc = db.collection("users").doc(`${user.email}`); + userDoc.get() + .then(function(doc) { + if (doc.exists) { + user.email = doc.data().email; + } + else { + res.status(500).send("No such doc"); + } + return; + }) + .then(function() { + firebase + .auth() + .signInWithEmailAndPassword(user.email, user.password) + .then((data) => { + return data.user.getIdToken(); + }) + .then((token) => { + return res.status(200).json({ token }); + }) + .catch((err) => { + console.error(err); + if (err.code === "auth/wrong-password" || err.code === "auth/invalid-email") { + return res + .status(403) + .json({ general: "Invalid credentials. Please try again." }); + } + return res.status(500).json({ error: err.code }); + }); + return; + }) + .catch(function(err) { + res.status(500).send(err); + }); + } + // Email/username field is username + else { + firebase .auth() .signInWithEmailAndPassword(user.email, user.password) .then((data) => { @@ -137,11 +170,12 @@ exports.login = (req, res) => { console.error(err); if (err.code === "auth/wrong-password" || err.code === "auth/invalid-email") { return res - .status(403) - .json({ general: "Invalid credentials. Please try again." }); + .status(403) + .json({ general: "Invalid credentials. Please try again." }); } return res.status(500).json({ error: err.code }); }); + } }; //Deletes user account From 657277bcad1c0893f8733620744ca96cf7773458 Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Sun, 27 Oct 2019 23:11:24 -0400 Subject: [PATCH 3/7] Username and user id now show in post data --- functions/handlers/post.js | 8 +- twistter-frontend/src/App.js | 1 - twistter-frontend/src/Writing_Microblogs.js | 14 +-- .../src/components/layout/NavBar.js | 87 +++++++++---------- 4 files changed, 51 insertions(+), 59 deletions(-) diff --git a/functions/handlers/post.js b/functions/handlers/post.js index e3d0c01..dc88e45 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -1,17 +1,17 @@ /* eslint-disable promise/always-return */ const admin = require('firebase-admin'); -exports.putPost = (req, res) => { +exports.putPost = (req, res) => { const newPost = { body: req.body.body, - userHandle: req.body.userHandle, + userHandle: req.user.handle, userImage: req.body.userImage, + userID: req.user.uid, microBlogTitle: req.body.microBlogTitle, createdAt: new Date().toISOString(), likeCount: 0, commentCount: 0, microBlogTopics: req.body.microBlogTopics - }; admin.firestore().collection('posts').add(newPost) @@ -27,7 +27,7 @@ exports.putPost = (req, res) => { }; exports.getallPostsforUser = (req, res) => { - admin.firestore().collection('posts').where('userHandle', '==', 'new user' ).get() + admin.firestore().collection('posts').where('userHandle', '==', req.userData.handle ).get() .then((data) => { let posts = []; data.forEach(function(doc) { diff --git a/twistter-frontend/src/App.js b/twistter-frontend/src/App.js index b767156..d2b880c 100644 --- a/twistter-frontend/src/App.js +++ b/twistter-frontend/src/App.js @@ -67,7 +67,6 @@ class App extends Component { - {/* */} diff --git a/twistter-frontend/src/Writing_Microblogs.js b/twistter-frontend/src/Writing_Microblogs.js index b1570d8..26f061d 100644 --- a/twistter-frontend/src/Writing_Microblogs.js +++ b/twistter-frontend/src/Writing_Microblogs.js @@ -5,7 +5,7 @@ import axios from 'axios'; class Writing_Microblogs extends Component { - + constructor(props) { super(props); this.state = { @@ -15,13 +15,13 @@ class Writing_Microblogs extends Component { characterCount: 250 }; - + this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.handleChangeforPost = this.handleChangeforPost.bind(this); this.handleChangeforTopics = this.handleChangeforTopics.bind(this); - + } handleChange(event) { @@ -33,10 +33,10 @@ class Writing_Microblogs extends Component { } handleSubmit(event) { - // alert('A title for the microblog was inputted: ' + this.state.title + '\nA microblog was posted: ' + this.state.value); + const postData = { - body: this.state.value, - userHandle: "new user", + body: this.state.value, + userImage: "bing-url", microBlogTitle: this.state.title, microBlogTopics: this.state.topics.split(', ') @@ -102,7 +102,7 @@ class Writing_Microblogs extends Component { ); } - + } export default Writing_Microblogs; \ No newline at end of file diff --git a/twistter-frontend/src/components/layout/NavBar.js b/twistter-frontend/src/components/layout/NavBar.js index 5f8d28d..f719ab8 100644 --- a/twistter-frontend/src/components/layout/NavBar.js +++ b/twistter-frontend/src/components/layout/NavBar.js @@ -1,7 +1,7 @@ /* eslint-disable */ import React, { Component } from 'react'; import { Link } from 'react-router-dom'; -// import PropTypes from 'prop-types'; +import PropTypes from 'prop-types'; // Material UI stuff import AppBar from '@material-ui/core/AppBar'; @@ -10,70 +10,63 @@ import Button from '@material-ui/core/Button'; import withStyles from "@material-ui/core/styles/withStyles"; // Redux stuff -// import { logoutUser } from '../../redux/actions/userActions'; -// import { connect } from 'react-redux'; - -// const styles = { -// form: { -// textAlign: "center" -// }, -// textField: { -// marginBottom: 30 -// }, -// pageTitle: { -// marginBottom: 40 -// }, -// button: { -// positon: "relative", -// marginBottom: 30 -// }, -// progress: { -// position: "absolute" -// } -// }; - - - +import { connect } from 'react-redux'; +const styles = { + form: { + textAlign: "center" + }, + textField: { + marginBottom: 30 + }, + pageTitle: { + marginBottom: 40 + }, + button: { + positon: "relative", + marginBottom: 30 + }, + progress: { + position: "absolute" + } +}; - export class Navbar extends Component { - render() { +export class Navbar extends Component { + render() { + const authenticated = this.props.user.authenticated; return ( - - } + {!authenticated && - } + {authenticated && - } + {authenticated && + } ) } } -// const mapStateToProps = (state) => ({ -// user: state.user -// }) +const mapStateToProps = (state) => ({ + user: state.user +}) -// const mapActionsToProps = { logoutUser }; +Navbar.propTypes = { + user: PropTypes.object.isRequired, + classes: PropTypes.object.isRequired +} -// Navbar.propTypes = { -// logoutUser: PropTypes.func.isRequired, -// user: PropTypes.object.isRequired, -// classes: PropTypes.object.isRequired -// } +export default connect(mapStateToProps)(withStyles(styles)(Navbar)); -// export default connect(mapStateToProps, mapActionsToProps)(withStyles(styles)(Navbar)); - -export default Navbar; +// export default Navbar; From 70a12dcca42a4b89c62b5718c25b11a6a5fb59ca Mon Sep 17 00:00:00 2001 From: shobhitm23 Date: Mon, 28 Oct 2019 00:21:29 -0400 Subject: [PATCH 4/7] Arrow-callback warning --- functions/handlers/post.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/functions/handlers/post.js b/functions/handlers/post.js index c14c61c..2ae8d3c 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -1,3 +1,4 @@ +/* eslint-disable prefer-arrow-callback */ /* eslint-disable promise/always-return */ const admin = require('firebase-admin'); exports.putPost = (req, res) => { @@ -41,3 +42,7 @@ exports.getallPostsforUser = (req, res) => { return res.status(500).json({error: 'Failed to fetch all posts written by specific user.'}) }) }; + +exports.getFilteredPosts = (req, res) => { + admin.firestore().collection('posts').where('userHandle', '==', 'new user').where('microBlogTopics', '==') +}; \ No newline at end of file From 9525ff7d0ababc8169dee2c5ac2a1d6b81635a63 Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Mon, 28 Oct 2019 17:57:03 -0400 Subject: [PATCH 5/7] Delete post works in Postman but not in actual database --- functions/handlers/users.js | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/functions/handlers/users.js b/functions/handlers/users.js index 87733bc..29e045b 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -168,7 +168,7 @@ exports.login = (req, res) => { }) .catch((err) => { console.error(err); - if (err.code === "auth/wrong-password" || err.code === "auth/invalid-email") { + if (err.code === "auth/invalid-email" || err.code === "auth/wrong-password") { return res .status(403) .json({ general: "Invalid credentials. Please try again." }); @@ -181,27 +181,52 @@ exports.login = (req, res) => { //Deletes user account exports.deleteUser = (req, res) => { var currentUser; - firebase.auth().onAuthStateChanged(function(user) { currentUser = user; if (currentUser) { + db.collection("posts").where("userId", "==", req.user.uid).get() + .then(function(userPosts) { + userPosts.forEach(function(post) { + post.delete() + .then(function() { + res.status(200).send("Successfully removed post from database."); + return; + }) + .catch(function(err) { + res.status(500).send("Failed to removed post from database.", err); + }); + }); + return; + }) + .then(function() { + res.status(200).send("Successfully removed all user's posts from database."); + return; + }) + .catch(function(err) { + res.status(500).send("Failed to remove all user's posts from database.", err); + }); + + + db.collection("users").doc(`${req.user.handle}`).delete() .then(function() { - res.status(200).send("Removed user from database."); + res.status(200).send("Sucessfully removed user from database."); return; }) .catch(function(err) { res.status(500).send("Failed to remove user from database.", err); }); + + currentUser.delete() .then(function() { - console.log("User successfully deleted."); + console.log("Successfully deleted user."); res.status(200).send("Deleted user."); return; }) .catch(function(err) { - console.log("Error deleting user.", err); + console.log("Failed to delete user.", err); res.status(500).send("Failed to delete user."); }); } @@ -228,8 +253,6 @@ exports.getProfileInfo = (req, res) => { // Updates the data in the database of the user who is currently logged in exports.updateProfileInfo = (req, res) => { - // TODO: Add functionality for adding/updating profile images - // Data validation const { valid, errors, profileData } = validateUpdateProfileInfo(req.body); if (!valid) return res.status(400).json(errors); From c482f567620aaf99de0906a543cd1b0b8b7939fc Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Mon, 28 Oct 2019 20:14:38 -0400 Subject: [PATCH 6/7] Invalid credential message displays after non-exisitng email is passed in --- functions/handlers/users.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/functions/handlers/users.js b/functions/handlers/users.js index 29e045b..6f2eac9 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -126,7 +126,7 @@ exports.login = (req, res) => { user.email = doc.data().email; } else { - res.status(500).send("No such doc"); + return res.status(403).json({ general: "Invalid credentials. Please try again." }); } return; }) @@ -142,17 +142,18 @@ exports.login = (req, res) => { }) .catch((err) => { console.error(err); - if (err.code === "auth/wrong-password" || err.code === "auth/invalid-email") { - return res - .status(403) - .json({ general: "Invalid credentials. Please try again." }); + if (err.code === "auth/user-not-found" || err.code === "auth/wrong-password") { + return res.status(403).json({ general: "Invalid credentials. Please try again." }); } return res.status(500).json({ error: err.code }); }); return; }) .catch(function(err) { - res.status(500).send(err); + if(!doc.exists) { + return res.status(403).json({ general: "Invalid credentials. Please try again." }); + } + return res.status(500).send(err); }); } // Email/username field is username @@ -168,10 +169,8 @@ exports.login = (req, res) => { }) .catch((err) => { console.error(err); - if (err.code === "auth/invalid-email" || err.code === "auth/wrong-password") { - return res - .status(403) - .json({ general: "Invalid credentials. Please try again." }); + if (err.code === "auth/user-not-found" || err.code === "auth/wrong-password") { + return res.status(403).json({ general: "Invalid credentials. Please try again." }); } return res.status(500).json({ error: err.code }); }); From 42b73632c020eef37aae9b03790ef0b2e586f78e Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Tue, 29 Oct 2019 10:34:09 -0400 Subject: [PATCH 7/7] Delete user's posts fully works now --- functions/handlers/users.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/functions/handlers/users.js b/functions/handlers/users.js index 6f2eac9..3db62dc 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -183,7 +183,22 @@ exports.deleteUser = (req, res) => { firebase.auth().onAuthStateChanged(function(user) { currentUser = user; if (currentUser) { - db.collection("posts").where("userId", "==", req.user.uid).get() + var post_query = db.collection("posts").where("userHandle", "==", req.user.handle); + post_query.get() + .then(function(myPosts) { + myPosts.forEach(function(doc) { + doc.ref.delete(); + }); + return; + }) + .then(function() { + res.status(200).send("Successfully removed all user's posts from database."); + return; + }) + .catch(function(err) { + res.status(500).send("Failed to remove all user's posts from database.", err); + }); + /*db.collection("posts").where("userHandle", "==", req.user.handle).get() .then(function(userPosts) { userPosts.forEach(function(post) { post.delete() @@ -196,14 +211,7 @@ exports.deleteUser = (req, res) => { }); }); return; - }) - .then(function() { - res.status(200).send("Successfully removed all user's posts from database."); - return; - }) - .catch(function(err) { - res.status(500).send("Failed to remove all user's posts from database.", err); - }); + })*/ @@ -221,7 +229,7 @@ exports.deleteUser = (req, res) => { currentUser.delete() .then(function() { console.log("Successfully deleted user."); - res.status(200).send("Deleted user."); + res.status(200).send("Sucessfully deleted user."); return; }) .catch(function(err) { @@ -230,8 +238,8 @@ exports.deleteUser = (req, res) => { }); } else { - console.log("Cannot get user."); - res.status(500).send("Cannot get user."); + console.log("Failed to deleter user or cannot get user."); + res.status(500).send("Failed to deleter user or cannot get user."); } }); };