0

I am trying to make use of Google Auth using OAuth2 passportjs strategy. I can neither check for existing user in the database, nor save a new one into the database. After logging in with Google, it seems I'm either not getting a response back or there is something wrong with my query.

I've tried async/await syntax, I've tried to console.log the profile object that should be sent back by Google and the console.log does not show up.

passport.use (
  new GoogleStrategy (
    {
      clientID: keys.googleClientID,
      clientSecret: keys.googleClientSecret,
      callbackURL: '/auth/google/callback',
    },
     async (accessToken, refreshToken, profile, done) => { 
      const existingUser = await knex('users').where({googleId: profile.id}).returning('*').then(data =>{
        return data;
      })
      const userParams = {googleId: profile.id}

      if(existingUser){
        done(null, existingUser)
      } else {
        knex ('users').insert(userParams).returning("*").then(data => {
          res.send(data)
        })
      }

Every time the callback is called, I get TypeError: Cannot read property 'query' of undefined error

Rich Churcher
  • 7,361
  • 3
  • 37
  • 60
  • 1
    Hi Boris. There is currently not enough detail in your question to see what the problem is. I strongly suspect that you're using Express and that the `req` (request) object is not available at this point in your code. However, if you could provide a https://stackoverflow.com/help/minimal-reproducible-example that would allow others to help you further! – Rich Churcher Nov 10 '19 at 19:13
  • Hello, could you show the full code? The error seems as though there is no model with the name users in your database. Kindly reconfirm that the model is available, spell properly and correctly imported here for query – Tolumide Nov 10 '19 at 19:29
  • [Drop the pointless `.then(data =>{ return data; })`](https://stackoverflow.com/q/41089122/1048572) – Bergi Nov 10 '19 at 20:21
  • Thank you for your responses! I've since fixed the issue, the issue appeared to be related with the order of middleware in my index.js and the callback function. I also realized serializeUser/deserializeUser functions were receiving an array so I had to select the first element of the array. – Boris Khvan Nov 13 '19 at 04:43

0 Answers0