Nodejs passport auth question?

How do I implement local aur google auth in nodejs application?

What have you tried? Where is your code? What problems have you encountered that you need help overcoming?

I’m tried google aur local auth but when I try local auth I’m able to access req.user object but when I login google auth creadentials I don’t have access req.user object so I can’t authorize user, I don’t know I’m really screwed up.

edit: I made two serialize or deserialize function and one of them I commented out.
code:

const User = new mongoose.model('gid', Schema);


app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true,
    store: MongoStore.create({ mongoUrl: 'mongodb://localhost:27017/setDB' }),
    // cookie: { secure: true }
    cookie: {
        maxAge: 1000 * 60 * 60 * 1
    }
}))


app.use(passport.initialize());
app.use(passport.session());
app.use(express.json());



// google auth setup


passport.serializeUser((user, done) => {
    done(null, user.id);
});


passport.deserializeUser((id, done) => {
    User.findById(id).then(user => {
        console.log("fuck that user: ", user)
        done(null, user);
    });
});





passport.use(new GoogleStrategy({
    clientID: 'client id',
    clientSecret: 'client secret',
    callbackURL: "http://localhost:3000/auth/google/callback"
},
    async function (accessToken, refreshToken, profile, done) {


        // check if user already exists in our own db
        User.findOne({ googleID: profile.id }).then((currentUser) => {
            if (currentUser) {
                // already have this user
                // console.log('user is: ', currentUser);
                done(null, currentUser);
            } else {
                // if not, create user in our db

                const user = new User({
                    name: profile.displayName,
                    photos: profile.photos[0].value,
                    email: profile.emails[0].value,
                    provider: 'google',
                    //now in the future searching on User.findOne({'facebook.id': profile.id } will match because of this next line
                    googleID: profile.id
                })
                user.save().then((newUser) => {
                    console.log('created new user: ', newUser);
                    done(null, newUser);
                });
            }
        });
    }));


app.get('/auth/google',
    passport.authenticate('google', { scope: ['profile', 'email'] }));
app.get('/auth/google/callback',
    passport.authenticate('google'),
    function (req, res) {
        res.send(req.user)
    });















// local auth


const customField = {
    usernameField: 'email',
    passwordField: 'password'
}


passport.use(new LocalStrategy(customField,
    async (email, password, done) => {
        try {
            // console.log("cup: ", "go live")
            const user = await User.findOne({ email });
            // console.log("**user**", user)
            if (!user) return done(null, false)
            // const isPasswordMatch = await bcrypt.compare(password, user.password);
            // if (!isPasswordMatch) return done(null, false)
            done(null, user)
        }
        catch (e) {
            done(e)
        }
    }
));

// passport.serializeUser((user, done) => {
//     console.log("##: ", user)
//     done(null, user.id);
// });

// passport.deserializeUser(async (id, done) => {
//     try {
//         const user = await UserModel.findById(id);
//         if (!user) return done(null, false)
//         done(null, user)
//     }
//     catch (e) {
//         done(e)
//     }
// });


app.post('/test_register', async (req, res) => {
    res.send(req.body)
    const user = req.body;
    try {
        new User({
            name: user.name,
            password: user.password,
            email: user.email
        }).save()
        res.send(user)
    }
    catch (e) {
        throw new Error(e)
    }
})


app.post("/test_login", (req, res, next) => {
    // console.log(req.body)
    passport.authenticate('local', (err, user, info) => {
        if (err) next(err);
        if (!user) return res.status(404).send("User not found")
        req.logIn(user, function (err) {
            if (err) { return next(err); }
            return res.send(user);
        });
    })(req, res, next)
})

app.get("/", (req, res) => {
    // console.log(req.session)
    // req.session.vc ? req.session.vc++ : req.session.vc = 1;
    // res.send("Req session: " + req.session.vc)
    res.send(req.user ? req.user : "user is not auth yet!")
});
app.use('/', require('./route'))

app.get('/logout', (req, res) => {
    req.logOut();
    res.send("success")
})

FYI you exposed your clientID and clientSecret tokens

I used wrong ClientID or ClientSecret here.