-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
86 lines (71 loc) · 2.86 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const morgan = require('morgan');
const multipart = require("connect-multiparty")();
// Import .env config vars for dev Environment
if (process.env.ENVIRONMENT === 'development') {
require('dotenv').config();
}
if(process.env.NODE_ENV !== 'production'){
require('dotenv').load();
}
// import auto jobs that run automatically on an interval:
require('./auto-jobs/updateRentalStatuses');
// Firebsae imports:
const firebase = require("firebase/app");
require("firebase/auth");
require("firebase/database");
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
}),
databaseURL: process.env.FIREBASE_DB_URL
});
const firebaseConfig = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.FIREBASE_DB_URL,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
};
firebase.initializeApp(firebaseConfig);
// Create Express server
const server = express();
server.use(express.json());
server.use(morgan('dev'));
server.use(cors());
server.use(helmet());
// Sanity check to see if base URL is live:
server.get('/',(req, res) => {
res.send("Server base URL is working...");
});
// Import API route/endpoint files:
const usersRoutes = require('./api/users'); // All CRUD endpoints for user-specific data
const toolsRoutes = require('./api/tools'); // All CRUD endpoints for tool-specific data
const rentalsRoutes = require('./api/rentals'); // All CRUD endpoints for rental-specific data
// Verify requests using Firebase-admin auth:
server.use(multipart, async(req,res) => {
console.log('server auth hit with req.body: ', req.body);
const idToken = req.headers.authorization;
console.log(idToken);
try {
await admin.auth().verifyIdToken(idToken) // verify the idToken of the incoming req
.then(decodedToken => { // get the decoded token back from Firebase
req.body.uid = decodedToken.uid; // add the uid from the decoded token to req.body
return req.next(); // return and move to the next part of the original req
});
}
catch(error) {
res.status(401).json({message: error.message});
}
})
// Connect API base routes to corresponding api folder:
server.use('/api/users', usersRoutes);
server.use('/api/tools', toolsRoutes);
server.use('/api/rentals', rentalsRoutes)
module.exports = server;