Home > Software engineering >  Why am I unable to access req.user.username while using passport.js?
Why am I unable to access req.user.username while using passport.js?

Time:11-04

I'm using passport.js for authentication, and forgive me if my phrasing is incorrect as I'm still new to this. After logging in via google, I redirect to the profile page. However, when I send the request Object (User), only the first parameter of the object is available to me through req.user (the id). I'll show the passport-setup, along with the routes and hopefully that should be enough information.

passport setup:

const passport = require('passport')
const GoogleStrategy = require('passport-google-oauth20')
const User = require('../models/User')
 
passport.serializeUser((user, done) => {
    done(null, user.id) 
})

passport.deserializeUser((id, done) => {
    User.findById(id).then((user) => {
        done(null, user.id) 
    })
})

passport.use(
    new GoogleStrategy({
    // options for the google strat
    callbackURL: '/auth/google/redirect',
    clientID: process.env.clientID,
    clientSecret: process.env.clientSecret
}, (accessToken, refreshToken, profile, done) => {
    // passport callback function

    User.findOne({googleId:profile.id}).then((currentUser) => {
        if(currentUser){
        // already have a user
        console.log('user is: '   currentUser)
        done(null, currentUser)
        } else {
            // if not, create user in db
            new User({
            username: profile.displayName,
            googleId: profile.id
        }).save().then((newUser) => {
            console.log('new user created'   newUser)
            done(null, newUser)
        })
    }
        
})

}))

auth route:

const express = require('express')
const router = express.Router()
const passport = require('passport')

// auth login
router.get('/login', (req, res) => {
    res.render('login')
})

// auth logout
router.get('/logout', (req, res) => {
    // handle the passport
    res.send('logging out')
})

// auth with google
router.get('/google', passport.authenticate('google', {
    scope: ['profile']
}));

// callback route for google to redirect to
router.get('/google/redirect', passport.authenticate('google'), (req, res) => {
    // res.send(req.user)
    res.redirect('/profile')
})

module.exports = router

If I type res.user.username within the last route, I get my username. But when I send the request to the redirect, req.user.username returns undefined, while req.user gives me the first parameter, which is the id. Looks something like this.

welcome 63638beb83e373e1acc7914c

Lastly, here is my profile route:

const router = require('express').Router()

router.get('/', (req, res) => {
    // req.user.username returns undefined
    res.send('this is your profile, welcome '   req.user)
})

module.exports = router

And Schema incase it's needed:

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const userSchema = new Schema({
    username: String,
    googleId: String
})

const User = mongoose.model("user", userSchema)

module.exports= User;

Any help is much appreciated!

CodePudding user response:

You should return entire user object here

passport.deserializeUser((id, done) => {
    User.findById(id).then((user) => {
        done(null, user) 
    })
})
  • Related