mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2026-03-10 13:15:05 +00:00
Code refactoring
This commit is contained in:
35
functions/util/FBAuth.js
Normal file
35
functions/util/FBAuth.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const {admin, db} = require('./admin');
|
||||
|
||||
// Acts as a middleman between the client and any function that you use it with
|
||||
// The function will only execute if the user is logged in, or rather, they have
|
||||
// a valid token
|
||||
module.exports = (req, resp, next) => {
|
||||
let idToken;
|
||||
|
||||
// Checking that the token exists in the header of the request
|
||||
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
|
||||
idToken = req.headers.authorization.split('Bearer ')[1];
|
||||
} else {
|
||||
console.error('No token found');
|
||||
return resp.status(403).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
// Checking that the token is valid in firebase
|
||||
admin.auth().verifyIdToken(idToken)
|
||||
.then(decodedToken => {
|
||||
req.user = decodedToken;
|
||||
console.log(decodedToken);
|
||||
return db.collection('users')
|
||||
.where('userId', '==', req.user.uid)
|
||||
.limit(1)
|
||||
.get();
|
||||
})
|
||||
.then(data => {
|
||||
req.user.handle = data.docs[0].data().handle; // Save username
|
||||
return next();
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('Error verifying token', err);
|
||||
return res.status(403).json(err);
|
||||
})
|
||||
};
|
||||
7
functions/util/admin.js
Normal file
7
functions/util/admin.js
Normal 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
9
functions/util/config.js
Normal 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"
|
||||
};
|
||||
17
functions/util/validator.js
Normal file
17
functions/util/validator.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const isEmpty = (str) => {
|
||||
if (str.trim() === '') return true;
|
||||
else return false;
|
||||
};
|
||||
|
||||
exports.validateUpdateProfileInfo = (profileData) => {
|
||||
let errors = {}
|
||||
|
||||
if (isEmpty(profileData.email)) {
|
||||
errors.email = "Must not be empty.";
|
||||
}
|
||||
|
||||
return {
|
||||
errors,
|
||||
valid: Object.keys(errors).length === 0 ? true : false
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user