I’m trying to add google authentiation in mern app after checking tutorials and searching I cannot find a suitable method
I set authentication stratergy with passort.js but dont know how to combine it with react
I tried react-google-login but it works on react and I dunno how to send user data to node for saving into database
miku86
2
Hey there,
what could any help look like?
Please provide us:
- some code (that doesn’t work)
- the material you’ve read/watched
You are talking about passport.js.
Did you read the docs of passport?
What has happened when you followed the instructions in the documentation??
this is my passport.js file
const GoogleStratergy = require('passport-google-oauth20').Strategy
const mongoose = require('mongoose')
const User = require('../../models/User')
module.exports = function(passport){
passport.use(new GoogleStratergy({
clientID:process.env.GOOGLE_CLIENT_ID,
clientSecret:process.env.GOOGLE_CLIENT_SECRET,
callbackURL:'/auth/google/callback'
},async (accessToken,refreshToken,profile,done)=>{
const newUser = {
googleId:profile.id,
displayName:profile.displayName,
firstName:profile.name.givenName,
lastName:profile.name.familyName,
image:profile.photos[0].value
}
try{
let user = await User.findOne({googleId:profile.id})
if(user){
done(null,user)
}
else{
user = await User.create(newUser)
done(null,user)
}
}
catch{
console.log(err)
}
}))
passport.serializeUser((user, done)=>{
done(null, user.id);
});
passport.deserializeUser((id, done)=>{
User.findById(id,(err, user)=>{
done(err, user);
});
});
}
it has no issues its working properly but I want to use with react how can I do that
tutorials that I watched
these dont have what I require cuz I want to sign in using react fornt end and then save that data using mongoose in node