I have an Express and Mongo blog app. I implemented the google authentication, but I don't know how to save the user id along with the post that the user creates.
This are the google settings:
const {OAuth2Client} = require('google-auth-library');
const client = new OAuth2Client(process.env.GOOGLE_CLIENT_ID);
const googleVerify = async( idToken = '' ) => {
const ticket = await client.verifyIdToken({
idToken,
audience: process.env.GOOGLE_CLIENT_ID
});
const { name,
picture: img,
email,
sub: id
} = ticket.getPayload();
return { name, img, email, id };
}
const googleSignIn = async(req, res = response) => {
try {
let googleUser = await googleVerify(req.body.id_token);
console.log(googleUser);
// console.log(googleUser);
} catch (error) {
res.status(400).json({
msg: 'Token de google no valido'
})
}
}
And this is the post route for an article:
router.post('/', upload.single('image'), async (req, res) => {
let body = req.body;
const article = new Article({
title: body.title,
description: body.description,
markdown: body.markdown,
img: req.file.filename
});
try {
await article.save();
} catch (error) {
console.log(error);
res.render('new', {article: article});
} finally {
res.redirect(`/articles/${article.id}`);
}
});
How can I access the logged user from that article route?
Thank you!!