-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckAuth.js
31 lines (26 loc) · 839 Bytes
/
checkAuth.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
const { OAuth2Client } = require('google-auth-library');
let CLIENT_ID = ``;
const client = new OAuth2Client(CLIENT_ID);
const checkAuthenticated = (req, res, next) => {
let token = req.cookies['session-token'];
let user = {};
async function verify() {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID, // Specify the CLIENT_ID of the Google auth that can be accessed by the backend
});
const payload = ticket.getPayload();
user.name = payload.name;
user.email = payload.email;
user.picture = payload.picture;
}
verify()
.then(() => {
req.user = user;
next();
})
.catch(() => {
res.redirect('/home')
})
}
module.exports = checkAuthenticated;