Skip to content

Instantly share code, notes, and snippets.

@ManishPoduval
Last active October 25, 2022 09:47
Show Gist options
  • Save ManishPoduval/33d738c67586fa9166f5a6ee6901f0bb to your computer and use it in GitHub Desktop.
Save ManishPoduval/33d738c67586fa9166f5a6ee6901f0bb to your computer and use it in GitHub Desktop.
const router = require("express").Router();
const UserModel = require('../models/User.model')
// The client makes a API request to this url sending the data in the body
router.post("/google/info", (req, res, next) => {
const {firstName, lastName, email, image, googleId} = req.body
// the name itself will include the last name
try {
// Create the user in the DB
UserModel.find({googleId})
.then((users) => {
console.log(users)
if (users.length) {
req.session.loggedInUser = users[0]
res.status(200).json({data: users[0]})
}
else {
UserModel.create({firstName, lastName, googleId, image, email})
.then((response) => {
// Save the loggedInInfo in the session
// We'll stick to using sessions just to not over complicate the students with tokens and cookies
req.session.loggedInUser = response
res.status(200).json({data: response})
})
}
})
}
catch(error) {
res.status(500).json({error: `${error}`})
}
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment