I am trying to implement a Google-based authentication using passport.js.
but I am getting Bad Request error
This is my main.js file
import express from "express"
import dotenv from "dotenv"
dotenv.config()
import { authRouter } from "./routes/auth"
const app = express()
const port = process.env.PORT
app.use('/auth', authRouter)
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
This is my authRoute
import { Router, Request, Response } from 'express'
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';
import passport from "passport"
const router = Router()
if (!process.env.GOOGLE_CLIENT_ID || !process.env.GOOGLE_CLIENT_SECRET) {
throw new Error('Missing Google OAuth credentials in environment variables');
}
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/callback",
},
(accessToken: any, refreshToken: any, profile: any, done: any) => {
console.log(`accessToken -> ${accessToken}, refreshToken -> ${refreshToken}, profile -> ${profile} `)
return done(null, profile)
})
)
router.use('/google',
passport.authenticate('google', { session: false, scope: ['profile', 'email'] })
)
router.get('/google/callback',
passport.authenticate('google', { failureRedirect: '/' }),
(req: Request, res: Response) => {
console.log("inside callback")
res.redirect('/')
}
)
export { router as authRouter }
Could anyone help me in identifying the problem?