-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (32 loc) · 1.1 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
const express = require('express');
const dotenv = require('dotenv');
const path = require('path');
const { OAuth2Client } = require('google-auth-library');
dotenv.config();
const client = new OAuth2Client(process.env.REACT_APP_GOOGLE_CLIENT_ID)
const app = express();
app.use(express.json());
const users = [];
function upsert(array, item) {
const i = array.findIndex((_item) => _item.email === item.email);
if(i > -1) array[i] = item;
else array.push(item);
}
app.post('/api/google-login', async ( req, res) => {
const { token } = req.body;
const ticket = await client.verifyIdToken({
idToken: token,
audience: process.env.CLIENT_ID
});
const { name, email, picture} = ticket.getPayload();
upsert(users, { name, email, picture });
res.status(201);
res.json({ name, email, picture })
});
app.use(express.static(path.join(__dirname, '/build')));
app.get('*', (req, res) =>
res.sendFile(path.join(__dirname, '/build/index.html'))
)
app.listen(process.env.PORT || 5000, () => {
console.log(`Server is ready at http://localhost:${process.env.PORT || 5000}`)
})