I am implementing google authentication on my blog application which is on react.
I am hitting this URL to invoke google authentication and my callback URL is /api/auth/google/callback.
However , i am running into two issues.
- I am getting redirect mismatch error , where the redirect_URI is showing as http://www.blog.frustratedprogrammer.in/ instead of https://blog.frustratedprogrammer.in/.
however , i have configured https version in my env variables.
- sometimes , i am getting
Missing required parameter: scopeerror.
I will provide my passport.js implementation along with my API routes , please look into this and ask for any additional info required.
//passport.js file
passport.use(
new Google(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "/api/auth/google/callback",
},
function (accessToken, refreshToken, profile, done) {
UserModel.findOne({ googleId: profile.id }, function (err, user) {
if (err) return done(err);
// my server side logic
}
)
);
router.get(
"/google",
passport.authenticate("google", { scope: ["profile", "email"] })
);
router.get(
"/google/callback",
passport.authenticate("google", { failureRedirect: "/auth/login/failed" }),
function (req, res) {
// Successful authentication, redirect home.
const { password, ...other } = req.user._doc;
req.session.user = other; // Store user details in session
res.redirect(process.env.CLIENT_ORIGIN);
}
);
and my client origin is CLIENT_ORIGIN=https://www.frustratedprogrammer.in/ and this is perfectly working fine on my localhost. only problem occurs when i use this on prod.
I have tried hardcoding the auth callback as mentioned in this stackoverflow solution but it is creating few more issues.
The issue with the redirect URI mismatch (http://www.blog.frustratedprogrammer.in/ instead of https://blog.frustratedprogrammer.in/) might be due to how your server is configured or how you're handling HTTPS. Make sure your server is correctly configured to handle HTTPS requests and redirects.
Check if there are any proxy servers in front of your Express server that might be causing the URL to change.