• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Node.js, Express 게시판 Error: Failed to serialize user into session

Error: Failed to serialize user into session

4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 글쓴이
  • #84109

    신미화
    참가자
    1. db에 login 컬렉션 만들고
    -----------------------
    _id: ObjectId('646ad188ac787715fced8d28')
    id: "test"
    pw: "test
    -----------------------
    
    2. 회원인증 라이브러리 3개 설치하고
    -----------------------
    const passport = require('passport');
    const LocalStrategy = require('passport-local').Strategy;
    const session = require('express-session');
    app.use(session({secret : '비밀코드', resave : true, saveUninitialized: false}));
    app.use(passport.initialize());
    app.use(passport.session());
    -----------------------
    
    passport.use(new LocalStrategy({
      usernameField: 'id',    //user가 입력한 id/pw항목이 뭔지 정의 (name속성)
      passwordField: 'pw',
      session: true,
      passReqToCallback: false,
    }, function (id, pw, done) {
        console.log(id, pw);
        db.collection('login').findOne({ id : id }, function (error, result) {
          if (error) return done(error)
          console.log('왜자꾸에러나1' + result + id + pw);
          if (!result) return done(null, false, { message: '존재하지않는 아이디요' }) 
          console.log('왜자꾸에러나22' + result+ id + pw);
          if (pw == result.pw) {
            return done(null, result) 
            
          } else {
            return done(null, false, { message: '비번틀렸어요' })
          }
        })
    }));
    app.get('/login', function(req, res){
      res.render('login.ejs')
    });
    app.post('/login', passport.authenticate('local', {
      failureRedirect : '/fail'   // 회원인증 실패하면 /fail로 이동~
    }), function(req, res){
      res.redirect('/')
    });
    app.get('/mypage', loginCheck,function(req, res){
      res.render('mypage.ejs')
    })
    function loginCheck(req, res, next){
      if(req.user){           // 로그인 후 세션이 있으면 req.user가 항상 있음
        next()
      } else {
        res.send('로그인한하셨는데요?')
      }
    }
    -----------------------
    
    /login 페이지에서 test / test 입력하면 아래처럼 에러 나오는데 왜그런 걸까요 ㅠ.ㅠ
    console에서 찍은 result가 [object object]가 문제는 아니겠죠? 오타는 없는거 같은데..... ㅠ.ㅠ
    도와주세요 ㅠ.ㅠ
    
    
    listening on 8080
    test test
    왜자꾸에러나1[object Object]testtest
    왜자꾸에러나22[object Object]testtest
    Error: Failed to serialize user into session
        at pass (D:. Shin\test123\codingapple\todoapp\node_modules\passport\lib\authenticator.js:278:19)
        at Authenticator.serializeUser (D:. Shin\test123\codingapple\todoapp\node_modules\passport\lib\authenticator.js:296:5)
        at D:. Shin\test123\codingapple\todoapp\node_modules\passport\lib\sessionmanager.js:33:10
        at Immediate._onImmediate (D:. Shin\test123\codingapple\todoapp\node_modules\express-session\session\store.js:54:5)
        at processImmediate (node:internal/timers:466:21)
    
    #84112

    신미화
    참가자
       console.log('왜자꾸에러나1', result , id , pw);
       console.log('왜자꾸에러나22' , result , id , pw);
    
    로 바꿨더니 result는 제대로 나오네요 ㅠ.ㅠ
    그럼 뭐가 문제일까요..
    
    -------------------------------------------------
    listening on 8080
    test test
    왜자꾸에러나1 { _id: 646ad188ac787715fced8d28, id: 'test', pw: 'test' } test test
    왜자꾸에러나22 { _id: 646ad188ac787715fced8d28, id: 'test', pw: 'test' } test test
    Error: Failed to serialize user into session
        at pass (D:. Shin\test123\codingapple\todoapp\node_modules\passport\lib\authenticator.js:278:19)
        at Authenticator.serializeUser (D:. Shin\test123\codingapple\todoapp\node_modules\passport\lib\authenticator.js:296:5)
        at D:. Shin\test123\codingapple\todoapp\node_modules\passport\lib\sessionmanager.js:33:10
        at Immediate._onImmediate (D:. Shin\test123\codingapple\todoapp\node_modules\express-session\session\store.js:54:5)
        at processImmediate (node:internal/timers:466:21)
    
    #84117

    codingapple
    키 마스터
    deserializeUser serializeUser 부분이 비어있어서 그런게 아닐까요
    #84128

    신미화
    참가자
    선생님 잘 되요 ㅠㅠㅠ 강의 제대로 다시 듣겠습니다 ㅠㅠㅠㅠㅠㅠ 감사합니다~~~
4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠