diff --git a/functions/handlers/post.js b/functions/handlers/post.js index a72be0e..4af7ae3 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -1,18 +1,19 @@ /* eslint-disable prefer-arrow-callback */ /* eslint-disable promise/always-return */ const admin = require('firebase-admin'); - exports.putPost = (req, res) => { + const newPost = { body: req.body.body, - userHandle: req.user.handle, + userHandle: req.userData.handle, userImage: req.body.userImage, - userID: req.user.uid, + userID: req.userData.userId, microBlogTitle: req.body.microBlogTitle, createdAt: new Date().toISOString(), likeCount: 0, commentCount: 0, microBlogTopics: req.body.microBlogTopics + }; admin.firestore().collection('posts').add(newPost) diff --git a/functions/handlers/users.js b/functions/handlers/users.js index 203aa58..335215a 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -7,6 +7,8 @@ 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, @@ -78,6 +80,7 @@ exports.signup = (req, res) => { userId, followedTopics: [] }; + handle2Email.set(userCred.handle, userCred.email); return db.doc(`/users/${newUser.handle}`).set(userCred); }) .then(() => { @@ -95,6 +98,7 @@ exports.signup = (req, res) => { exports.login = (req, res) => { const user = { email: req.body.email, + handle: req.body.handle, password: req.body.password }; @@ -103,63 +107,25 @@ 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,}))$/; - // Checks if email/username field is empty + // Email check if (user.email.trim() === "") { errors.email = "Email must not be blank."; } + else if (!user.email.match(emailRegEx)) { + user.email = handle2Email.get(user.email); + } - // Checks if password field is empty + // Password check if (user.password.trim() === "") { errors.password = "Password must not be blank."; } - // Checks if any of the above two errors were found + // Checking if any errors have been raised if (Object.keys(errors).length > 0) { return res.status(400).json(errors); } - // 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 { - return res.status(403).json({ general: "Invalid credentials. Please try again." }); - } - 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/user-not-found" || err.code === "auth/invalid-email" || 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) { - 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 - else { - firebase + firebase .auth() .signInWithEmailAndPassword(user.email, user.password) .then((data) => { @@ -170,65 +136,49 @@ exports.login = (req, res) => { }) .catch((err) => { console.error(err); - if (err.code === "auth/user-not-found" || err.code === "auth/invalid-email" || err.code === "auth/wrong-password") { + if (err.code === "auth/wrong-password" || err.code === "auth/invalid-email" || err.code === "auth/user-not-found") { 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 exports.deleteUser = (req, res) => { var currentUser; + firebase.auth().onAuthStateChanged(function(user) { currentUser = user; if (currentUser) { - 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; - }) + /*db.collection("users").doc(`${currentUser.handle}`).delete() .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("Sucessfully removed user from database."); + 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() { - console.log("Successfully deleted user."); - res.status(200).send("Sucessfully deleted user."); + console.log("User successfully deleted."); + res.status(200).send("Deleted user."); return; }) .catch(function(err) { - console.log("Failed to delete user.", err); + console.log("Error deleting user.", err); res.status(500).send("Failed to delete user."); }); } else { - console.log("Failed to deleter user or cannot get user."); - res.status(500).send("Failed to deleter user or cannot get user."); + console.log("Cannot get user."); + res.status(500).send("Cannot get user."); } }); }; @@ -249,6 +199,8 @@ 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); if (!valid) return res.status(400).json(errors); diff --git a/functions/index.js b/functions/index.js index 33ce166..3be20a4 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", fbAuth, deleteUser); +app.delete("/delete", deleteUser); app.get("/getUser", fbAuth, getUserDetails); diff --git a/twistter-frontend/src/App.js b/twistter-frontend/src/App.js index 0420d45..2335b20 100644 --- a/twistter-frontend/src/App.js +++ b/twistter-frontend/src/App.js @@ -75,6 +75,7 @@ class App extends Component { + {/* */} diff --git a/twistter-frontend/src/Writing_Microblogs.js b/twistter-frontend/src/Writing_Microblogs.js index 0734d1c..6ddb974 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,7 +33,7 @@ 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, userImage: "bing-url", @@ -101,7 +101,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 38434b5..ceab08f 100644 --- a/twistter-frontend/src/components/layout/NavBar.js +++ b/twistter-frontend/src/components/layout/NavBar.js @@ -31,9 +31,13 @@ const styles = { position: "absolute" } }; + + + + -export class Navbar extends Component { - render() { + export class Navbar extends Component { + render() { const authenticated = this.props.user.authenticated; return ( @@ -50,9 +54,11 @@ export class Navbar extends Component { {authenticated && } - {authenticated && } + */} ) diff --git a/twistter-frontend/src/pages/Delete.js b/twistter-frontend/src/pages/Delete.js index eafeecf..ab610b1 100644 --- a/twistter-frontend/src/pages/Delete.js +++ b/twistter-frontend/src/pages/Delete.js @@ -7,8 +7,7 @@ import Button from "@material-ui/core/Button"; import withStyles from "@material-ui/core/styles/withStyles"; // Redux stuff -//import { logoutUser } from "../redux/actions/userActions"; -import { deleteUser } from "../redux/actions/userActions"; +import { logoutUser } from "../redux/actions/userActions"; import { connect } from "react-redux"; const styles = { @@ -33,8 +32,7 @@ const styles = { export class Delete extends Component { componentDidMount() { - //this.props.logoutUser(); - this.props.deleteUser(); + this.props.logoutUser(); this.props.history.push('/'); } @@ -47,12 +45,10 @@ const mapStateToProps = (state) => ({ user: state.user }); -//const mapActionsToProps = { logoutUser }; -const mapActionsToProps = { deleteUser }; +const mapActionsToProps = { logoutUser }; Delete.propTypes = { - //logoutUser: PropTypes.func.isRequired, - deleteUser: PropTypes.func.isRequired, + logoutUser: PropTypes.func.isRequired, user: PropTypes.object.isRequired, classes: PropTypes.object.isRequired };