-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (103 loc) · 2.82 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require('dotenv').config();
const express = require('express');
const passport = require('passport');
const path = require('path');
const cookieSession = require('cookie-session');
const cors = require('cors');
require('./passport-setup');
//firebase admin sdk
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: JSON.parse(process.env.FIREBASE_PRIVATE_KEY),
}),
});
const db = admin.firestore();
const app = express();
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
//cokie session
app.use(
cookieSession({
name: 'cromato-api-session',
keys: ['key1', 'key2'],
})
);
//passportjs
app.use(passport.initialize());
app.use(passport.session());
// Serve the static files from the React app
app.use(express.static(path.join(__dirname, 'cromato-client-reactjs/build')));
const isAuth = (req, res, next) => {
//console.log('req-user:', req.user);
if (req.user) next();
else res.sendStatus(401);
};
app.get('/api', (req, res) => {
if (req.user) res.redirect('/api/statistics');
//we're auth
else res.redirect('/api/auth'); //we need auth
});
app.get('/api/loggedout', (req, res) =>
res.json({ op: 'Successful', message: 'You sign out' })
);
app.get('/api/signout', (req, res) => {
req.session = null; // destroy session
req.logout();
res.redirect('/api/loggedout');
});
app.get('/api/fail', (req, res) =>
res.json({
op: 'Error',
messge: 'Auth failed',
})
);
app.get('/api/statistics', isAuth, (req, res) => {
(async () => {
const statisticSnapshot = await db
.collection('users')
.doc(req.user.id)
.get();
const tasksSnapshot = await db
.collection(`users/${req.user.id}/tasks`)
.get();
if (statisticSnapshot.exists)
console.log('statistic-', statisticSnapshot.data());
let tasksData = [];
tasksSnapshot.forEach((doc) => {
tasksData.push(doc.data());
});
res.json({
status: 'account active',
reponseStatus: 200,
user: req.user,
statistics: statisticSnapshot.data(),
tasksData: tasksData,
});
})();
});
app.get(
'/api/auth',
passport.authenticate('google', { scope: ['profile', 'email'] })
);
app.get(
'/api/auth/callback',
passport.authenticate('google', { failureRedirect: 'api/fail' }),
function (req, res) {
// Successful authentication, redirect to statistics.
res.redirect('/api/statistics');
}
);
// Handles any requests that don't match the ones above
app.get('*', (req, res) => {
res.sendFile(
path.join(__dirname + '/cromato-client-reactjs/build/index.html')
);
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log('App is listening on port ' + port);
});