Skip to content

Commit

Permalink
user added to each msg
Browse files Browse the repository at this point in the history
  • Loading branch information
ritik3131 committed Mar 2, 2022
1 parent 27c6d9a commit 064abf8
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 58 deletions.
29 changes: 20 additions & 9 deletions backend/controller/userController.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const { OAuth2Client } = require("google-auth-library");
const mongoose = require("mongoose");
const userModel = require("../models/userModel");

const client = new OAuth2Client(process.env.CLIENT_ID);

exports.getCurrentUser = async (req, res) => {
// const data = req.user;
console.log(process.env.CLIENT_ID);
// console.log("user", req.isAuthenticated(), req.session);
// res.status(200).json(data);
const data = req.user;
if (data)
res
.status(200)
.json({ name: data.name, mailId: data.mailId, isAuth: true });
else res.status(200).json({ isAuth: false });
};

exports.googleLogin = async (req, res) => {
Expand All @@ -16,30 +19,38 @@ exports.googleLogin = async (req, res) => {
idToken: tokenId,
requiredAudience: process.env.CLIENT_ID,
});

try {
const { email_verified, name, email, picture } = response.payload;
if (email_verified) {
const user=await userModel.findOne({ mailId: email }).exec();
req.session.isAuthenticated = true;
const user = await userModel.findOne({ mailId: email }).exec();
if (user) {
const { name, mailId } = user;
return res.status(200).json( { name, mailId });
req.session.user = user;
req.session.save();
return res.status(200).json({ name, mailId, isAuth: true });
} else {
const newUser = new userModel({
name,
mailId: email,
image: picture,
});
await newUser.save();
return res.status(200).json({name, mailId: email });
req.session.user = newUser;
req.session.save();
return res.status(200).json({ name, mailId: email, isAuth: true });
}
} else {
return res.status(400).json({
error: "Something went wrong",
message: "Something went wrong",
isAuth: true,
});
}
} catch (err) {
return res.status(400).json({
error: "Something went wrong",
message: "Something went wrong",
isAuth: true,
});
}
};
14 changes: 14 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ app.use(
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URL,
collectionName:'sessions'
}),
cookie: {
secure:false,
Expand All @@ -98,6 +99,19 @@ app.use(
})
);

app.use((req, res, next) => {
//console.log("session ",req.session);
if (!req.session.user) return next();
User.findById(req.session.user._id)
.then((user) => {
console.log("user",user)
req.user = user;
next();
})
.catch((err) => {
next(new Error(err));
});
});

app.use("/api/v1/message", messageRouter);
app.use("/api/v1/room", roomRouter);
Expand Down
162 changes: 156 additions & 6 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@fortawesome/fontawesome-svg-core": "^1.2.36",
"@fortawesome/react-fontawesome": "^0.1.16",
"@mui/icons-material": "^5.3.0",
"@reduxjs/toolkit": "^1.8.0",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
Expand All @@ -16,7 +17,9 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-google-login": "^5.2.2",
"react-redux": "^7.2.6",
"react-scripts": "5.0.0",
"redux": "^4.1.2",
"web-vitals": "^2.1.3"
},
"scripts": {
Expand Down
Loading

0 comments on commit 064abf8

Please sign in to comment.