Skip to content

Commit f6de6c8

Browse files
authored
Merge branch 'Project-Developers-2k24:dev' into dev
2 parents 1d57ae1 + 311cfdd commit f6de6c8

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/controllers/userController.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,51 @@ const uploadBooks = async (req, res) => {
191191
}
192192
}
193193
};
194+
async function googleAuth(req, res) {
195+
try {
196+
const { profile } = req.body;
197+
console.log(profile);
198+
if (!profile?.email) {
199+
return res
200+
.status(400)
201+
.json({ status: "failed", message: "Email is required" });
202+
}
203+
204+
let user = await User.findOne({ email: profile.email });
205+
console.log(user)
206+
if (!user) {
207+
user = new User({
208+
googleId: profile.id,
209+
username: profile.givenName,
210+
email: profile.email,
211+
avatar: profile.photo || "",
212+
});
213+
await user.save();
214+
}
215+
const accessToken = generateToken(
216+
user._id,
217+
process.env.JWT_SECRET,
218+
"1d"
219+
);
220+
const refreshToken = generateToken(
221+
user._id,
222+
process.env.JWT_REFRESH_SECRET,
223+
"7d"
224+
);
225+
res.status(200).json({
226+
message: "Login successfully",
227+
status: true,
228+
accessToken,
229+
refreshToken,
230+
user: user,
231+
});
232+
} catch (error) {
233+
console.log("Error in googleAuth:", error);
234+
res
235+
.status(500)
236+
.json({ status: "failed", message: "Unable to Google Auth" });
237+
}
238+
}
194239

195240
async function userRegistration(req, res) {
196241
const { username, email, password, isVerified } = req.body;
@@ -717,6 +762,7 @@ module.exports = {
717762
askChatBot,
718763
uploadBooks,
719764
deleteBook,
720-
userData,
765+
googleAuth,
721766
userIsDisable,
767+
userData,
722768
};

src/routes/userRoutes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ router.get(
277277
"/google",
278278
passport.authenticate("google", { scope: ["profile", "email"] })
279279
);
280+
router.post("/googleCreate", UserController.googleAuth);
280281

281282
//admin get data
282283
router.get("/userData", authMiddleware, UserController.userData);

0 commit comments

Comments
 (0)