diff --git a/functions/handlers/post.js b/functions/handlers/post.js index e3d0c01..1db0144 100644 --- a/functions/handlers/post.js +++ b/functions/handlers/post.js @@ -4,8 +4,9 @@ exports.putPost = (req, res) => { const newPost = { body: req.body.body, - userHandle: req.body.userHandle, + userHandle: req.userData.handle, userImage: req.body.userImage, + userID: req.userData.userId, microBlogTitle: req.body.microBlogTitle, createdAt: new Date().toISOString(), likeCount: 0, @@ -27,7 +28,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/functions/handlers/users.js b/functions/handlers/users.js index 1af539d..335215a 100644 --- a/functions/handlers/users.js +++ b/functions/handlers/users.js @@ -77,7 +77,8 @@ exports.signup = (req, res) => { email: newUser.email, handle: newUser.handle, createdAt: newUser.createdAt, - userId + userId, + followedTopics: [] }; handle2Email.set(userCred.handle, userCred.email); return db.doc(`/users/${newUser.handle}`).set(userCred); diff --git a/functions/package-lock.json b/functions/package-lock.json index 439a2e2..acfe9ce 100644 --- a/functions/package-lock.json +++ b/functions/package-lock.json @@ -535,6 +535,11 @@ "integrity": "sha512-cBhpre4ma+U0T1oM5fXg7Dy1Jw7zzwv7lt/GoCpr+hDQJoYnKVPLL4dCvSEFMmQurOQvSrwT7SL/DAlhBI97RQ==", "dev": true }, + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==" + }, "ansi-styles": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", @@ -1190,11 +1195,18 @@ "progress": "^2.0.0", "regexpp": "^2.0.1", "semver": "^5.5.1", + "strip-ansi": "^4.0.0", "strip-json-comments": "^2.0.1", "table": "^5.2.3", "text-table": "^0.2.0" }, "dependencies": { + "ansi-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", + "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=", + "dev": true + }, "debug": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", @@ -1209,6 +1221,15 @@ "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", "dev": true + }, + "strip-ansi": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", + "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", + "dev": true, + "requires": { + "ansi-regex": "^3.0.0" + } } } }, @@ -2355,6 +2376,7 @@ "mute-stream": "0.0.7", "run-async": "^2.2.0", "rxjs": "^6.4.0", + "string-width": "^2.1.0", "strip-ansi": "^5.1.0", "through": "^2.3.6" }, @@ -2403,6 +2425,12 @@ "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", "optional": true }, + "is-fullwidth-code-point": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=", + "dev": true + }, "is-obj": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-2.0.0.tgz", @@ -3252,7 +3280,8 @@ "dev": true, "requires": { "ansi-styles": "^3.2.0", - "astral-regex": "^1.0.0" + "astral-regex": "^1.0.0", + "is-fullwidth-code-point": "^2.0.0" } }, "snakeize": { @@ -3292,12 +3321,47 @@ "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz", "integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo=" }, + "string-width": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", + "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==", + "dev": true, + "requires": { + "is-fullwidth-code-point": "^2.0.0", + "strip-ansi": "^4.0.0" + }, + "dependencies": { + "ansi-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz", + "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=", + "dev": true + }, + "strip-ansi": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", + "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=", + "dev": true, + "requires": { + "ansi-regex": "^3.0.0" + } + } + } + }, "string_decoder": { "version": "0.10.31", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=", "optional": true }, + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "requires": { + "ansi-regex": "^4.1.0" + } + }, "strip-json-comments": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", @@ -3344,6 +3408,7 @@ "dev": true, "requires": { "emoji-regex": "^7.0.1", + "is-fullwidth-code-point": "^2.0.0", "strip-ansi": "^5.1.0" } }, diff --git a/functions/package.json b/functions/package.json index 2e308b3..29b3e89 100644 --- a/functions/package.json +++ b/functions/package.json @@ -16,7 +16,8 @@ "axios": "^0.19.0", "firebase": "^6.6.2", "firebase-admin": "^8.6.0", - "firebase-functions": "^3.1.0" + "firebase-functions": "^3.1.0", + "strip-ansi": "^5.2.0" }, "devDependencies": { "eslint": "^5.12.0", diff --git a/twistter-frontend/src/App.js b/twistter-frontend/src/App.js index 992347d..e4c5270 100644 --- a/twistter-frontend/src/App.js +++ b/twistter-frontend/src/App.js @@ -19,7 +19,7 @@ import { logoutUser, getUserData } from "./redux/actions/userActions"; // Components import AuthRoute from "./util/AuthRoute"; -axios.defaults.baseURL = "http://localhost:5006/twistter-e4649/us-central1/api"; +// axios.defaults.baseURL = 'http://localhost:5006/twistter-e4649/us-central1/api'; // Pages import home from "./pages/Home"; @@ -36,14 +36,19 @@ const theme = createMuiTheme(themeObject); const token = localStorage.FBIdToken; if (token) { - const decodedToken = jwtDecode(token); - if (decodedToken.exp * 1000 < Date.now()) { + try { + const decodedToken = jwtDecode(token); + if (decodedToken.exp * 1000 < Date.now()) { + store.dispatch(logoutUser()); + window.location.href = "/login"; + } else { + store.dispatch({ type: SET_AUTHENTICATED }); + axios.defaults.headers.common["Authorization"] = token; + store.dispatch(getUserData()); + } + } catch (invalidTokenError) { store.dispatch(logoutUser()); window.location.href = "/login"; - } else { - store.dispatch({ type: SET_AUTHENTICATED }); - axios.defaults.headers.common["Authorization"] = token; - store.dispatch(getUserData()); } } diff --git a/twistter-frontend/src/Writing_Microblogs.js b/twistter-frontend/src/Writing_Microblogs.js index 8d3dced..730dcde 100644 --- a/twistter-frontend/src/Writing_Microblogs.js +++ b/twistter-frontend/src/Writing_Microblogs.js @@ -23,8 +23,30 @@ class Writing_Microblogs extends Component { this.setState({ title: event.target.value }); } - handleChangeforTopics(event) { - this.setState({ topics: event.target.value }); + 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", + microBlogTitle: this.state.title, + microBlogTopics: this.state.topics.split(", ") + }; + const headers = { + headers: { "Content-Type": "application/json" } + }; + + axios + .post("/putPost", postData, headers) + .then(res => { + alert("Post was shared successfully!"); + console.log(res.data); + }) + .catch(err => { + alert("An error occured."); + console.error(err); + }); + event.preventDefault(); + this.setState({ value: "", title: "", characterCount: 250, topics: "" }); } handleSubmit(event) {