diff --git a/functions/handlers/post.js b/functions/handlers/post.js new file mode 100644 index 0000000..71b97eb --- /dev/null +++ b/functions/handlers/post.js @@ -0,0 +1,27 @@ +/* eslint-disable promise/always-return */ +exports.putPost = (req, res) => { + if (req.body.body.trim() === '') { + return res.status(400).json({ body: 'Body must not be empty!'}); + } + + const newPost = { + body: req.body.body, + userHandle: req.user.handle, + userImage: req.user.imageUrl, + createdAt: new Date().toISOString(), + likeCount: 0, + commentCount: 0 + }; + + db.collection('post').add(newPost) + .then((doc) => { + const resPost = newPost; + resPost.postId = doc.id; + res.json(resPost); + }) + .catch((err) => { + res.status(500).json({ error: 'something is wrong'}); + console.error(err); + }); +}; + diff --git a/functions/handlers/users.js b/functions/handlers/users.js new file mode 100644 index 0000000..2b624b0 --- /dev/null +++ b/functions/handlers/users.js @@ -0,0 +1,33 @@ +exports.getUserDetails = (req, res) => { + let userData = {}; + db.doc('/users/${req.params.handle}').get().then((doc) => { + if (doc.exists) { + userData.user = doc.data(); + return db.collection('post').where('userHandle', '==', req.params.handle) + .orderBy('createdAt', 'desc').get(); + } else { + return res.status(404).json({ + error: 'User not found' + }); + } + }) + .then((data) => { + userData.posts = []; + data.forEach((doc) => { + userData.posts.push({ + body: doc.data().body, + createAt: doc.data().createAt, + userHandle: doc.data().userHandle, + userImage: doc.data().userImage, + likeCount: doc.data().likeCount, + commentCount: doc.data().commentCount, + postId: doc.id + }); + }); + return res.json(userData); + }) + .catch((err) => { + console.error(err); + return res.status(500).json({ error: err.code}); + }); +}; \ No newline at end of file diff --git a/functions/util/admin.js b/functions/util/admin.js new file mode 100644 index 0000000..d6c3c18 --- /dev/null +++ b/functions/util/admin.js @@ -0,0 +1,7 @@ +const admin = require('firebase-admin'); + +admin.initializeApp(); + +const db = admin.firestore(); + +module.exports = { admin, db }; \ No newline at end of file diff --git a/functions/util/config.js b/functions/util/config.js new file mode 100644 index 0000000..903fed5 --- /dev/null +++ b/functions/util/config.js @@ -0,0 +1,9 @@ +module.exports = { + apiKey: "AIzaSyCvsWetg4qFdsPGfJ3LCw_QaaYzoan7Q34", + authDomain: "twistter-e4649.firebaseapp.com", + databaseURL: "https://twistter-e4649.firebaseio.com", + projectId: "twistter-e4649", + storageBucket: "twistter-e4649.appspot.com", + messagingSenderId: "20131817365", + appId: "1:20131817365:web:633c95fb08b16d4526b89c" +}; \ No newline at end of file diff --git a/functions/util/fbAuth.js b/functions/util/fbAuth.js new file mode 100644 index 0000000..24369a6 --- /dev/null +++ b/functions/util/fbAuth.js @@ -0,0 +1,28 @@ +const { admin, db } = require('./admin'); + +module.exports = (req, res, next) => { + let idToken; + if (req.headers.authorization) { + idToken = req.headers.authorization; + } else { + console.error('No token found'); + return res.status(403).json({ error: 'Unauthorized'}); + } + + admin.auth().verifyIdToken(idToken) + .then((decodedToken) => { + req.user = decodedToken; + return db.collection('users').where('userId', '==', req.user.uid) + .limit(1) + .get(); + }) + .then((data) => { + req.user.handle = data.docs[0].data().handle; + req.user.imageUrl = data.docs[0].data().imageUrl; + return next(); + }) + .catch((err) => { + console.error('Error while verifying token ', err); + return res.status(403).json(err); + }); +}; \ No newline at end of file diff --git a/functions/util/validator.js b/functions/util/validator.js new file mode 100644 index 0000000..e69de29 diff --git a/twistter-frontend/public/404.html b/twistter-frontend/public/404.html new file mode 100644 index 0000000..829eda8 --- /dev/null +++ b/twistter-frontend/public/404.html @@ -0,0 +1,33 @@ + + + + + + Page Not Found + + + + +
+

404

+

Page Not Found

+

The specified file was not found on this website. Please check the URL for mistakes and try again.

+

Why am I seeing this?

+

This page was generated by the Firebase Command-Line Interface. To modify it, edit the 404.html file in your project's configured public directory.

+
+ + diff --git a/twistter-frontend/src/images/twistter-logo.png b/twistter-frontend/src/images/twistter-logo.png new file mode 100644 index 0000000..c28e365 Binary files /dev/null and b/twistter-frontend/src/images/twistter-logo.png differ