| 1 | import express from "express"; |
| 2 | import passport from "passport"; |
| 3 | |
| 4 | const router = express.Router(); |
| 5 | |
| 6 | router.get( |
| 7 | "/google", |
| 8 | passport.authenticate("google", { scope: ["profile", "email"] }) |
| 9 | ); |
| 10 | |
| 11 | router.get( |
| 12 | "/google/callback", |
| 13 | passport.authenticate("google", { failureRedirect: "/login" }), |
| 14 | (req, res) => { |
| 15 | res.redirect(`${process.env.CLIENT_URL}/dashboard`); |
| 16 | } |
| 17 | ); |
| 18 | |
| 19 | router.get("/current-user", (req, res) => { |
| 20 | if (req.isAuthenticated()) { |
| 21 | res.json(req.user); |
| 22 | } else { |
| 23 | res.status(401).json({ error: "Unauthorized" }); |
| 24 | } |
| 25 | }); |
| 26 | |
| 27 | router.get("/logout", (req, res, next) => { |
| 28 | req.logout((err) => { |
| 29 | if (err) { |
| 30 | return next(err); |
| 31 | } |
| 32 | res.status(200).json({ status: "ok" }); |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | export default router; |