added folders to organize files

This commit is contained in:
Leon Liang 2019-09-26 01:54:51 -04:00
parent 8fa03700b1
commit 70037a5342
8 changed files with 137 additions and 0 deletions

View File

@ -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);
});
};

View File

@ -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});
});
};

7
functions/util/admin.js Normal file
View File

@ -0,0 +1,7 @@
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
module.exports = { admin, db };

9
functions/util/config.js Normal file
View File

@ -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"
};

28
functions/util/fbAuth.js Normal file
View File

@ -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);
});
};

View File

View File

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page Not Found</title>
<style media="screen">
body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
#message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px 16px; border-radius: 3px; }
#message h3 { color: #888; font-weight: normal; font-size: 16px; margin: 16px 0 12px; }
#message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; }
#message h1 { font-size: 22px; font-weight: 300; color: rgba(0,0,0,0.6); margin: 0 0 16px;}
#message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
#message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
#message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
#load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
@media (max-width: 600px) {
body, #message { margin-top: 0; background: white; box-shadow: none; }
body { border-top: 16px solid #ffa100; }
}
</style>
</head>
<body>
<div id="message">
<h2>404</h2>
<h1>Page Not Found</h1>
<p>The specified file was not found on this website. Please check the URL for mistakes and try again.</p>
<h3>Why am I seeing this?</h3>
<p>This page was generated by the Firebase Command-Line Interface. To modify it, edit the <code>404.html</code> file in your project's configured <code>public</code> directory.</p>
</div>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB