How to use the deserializeUser function from passport

Find comprehensive JavaScript passport.deserializeUser code examples handpicked from public code repositorys.

87
88
89
90
91
92
93
94
95
96

passport.serializeUser(function (sessionInfo, cb) {
  cb(null, sessionInfo);
});

passport.deserializeUser(async function (sessionInfo, cb) {
  cb(null, sessionInfo);
});

// Prep the web server
fork icon1
star icon2
watch icon5

+ 4 other calls in file

10
11
12
13
14
15
16
17
18
19
    console.log('serr ', user.email);
    done(null, user); // 여기의 user가 deserializeUser의 첫 번째 매개변수로 이동
});

// 페이지를 방문할때마다 세션스토어에서 가져와서
passport.deserializeUser((user, done) => {
    // 매개변수 user는 serializeUser의 done의 인자 user를 받은 것
    console.log('deserr ', user.email);

    done(null, user);   // 여기의 user가 req.user가 됨
fork icon1
star icon1
watch icon0

13
14
15
16
17
18
19
20
21
22
app.use(passport.initialize(undefined))
// passport.session acts as a middlewate to alter the req object and change the 'user',
// value that is currently the session id into the true deserialized user object
app.use(passport.session(undefined))
passport.serializeUser((user, done) => done(null, user))
passport.deserializeUser((user, done) => done(null, user))
// quando a path é apenas /signup, vai ser chamada a função createUserForm
router.get('/signup', createUserForm)
router.post('/signup', createUser)
router.get('/login', loginForm)
fork icon0
star icon0
watch icon1

68
69
70
71
72
73
74
75
76
77

passport.serializeUser((user, done) => {
  if (!user) return; // In serialization, if the value received is false, as we saw above, it means that the information did not arrive in its entirety, that is, it is a repeated request and therefore it did not get all the information, so we must return to avoid serialization errors
  return done(null, user);
});
passport.deserializeUser((obj, done) => done(null, obj));

passport.use(
  new Strategy(
    {
fork icon0
star icon0
watch icon1

77
78
79
80
81
82
83
84
85
86
  process.nextTick(function() {
    cb(null, { id: user.id, username: user.username });
  });
});

passport.deserializeUser(function(user, cb) {
  process.nextTick(function() {
    return cb(null, user);
  });
});
fork icon0
star icon0
watch icon1

+ 3 other calls in file

58
59
60
61
62
63
64
65
66
67

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

passport.deserializeUser(async (id, done) => {
  try {
    const user = await User.findById(id);
    done(null, user);
  } catch (err) {
fork icon0
star icon0
watch icon0

59
60
61
62
63
64
65
66
67
68

// 設定序列化與反序列化
passport.serializeUser((user, done) => {
  done(null, user.id)
})
passport.deserializeUser((id, done) => {
  User.findById(id)
    .lean()
    .then(user => done(null, user)) 
    .catch(err => done(err, null)) 
fork icon0
star icon0
watch icon0