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

home2 게시판 Node.js, Express 게시판 요청.user 출력했을 때 undefined

요청.user 출력했을 때 undefined

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

    김예지
    참가자
    passport.use(new LocalStrategy(async (입력한아이디, 입력한비번, cb) => {
      let result = await db.collection('user').findOne({ username : 입력한아이디})
      if (!result) {
        return cb(null, false, { message: '아이디 DB에 없음' })
      }
      if (result.password == 입력한비번) {
        return cb(null, result)
      } else {
        return cb(null, false, { message: '비번불일치' });
      }
    }))
    passport.serializeUser((user, done) => {
      //console.log(user)
      process.nextTick(() => {
        done(null, { id: user._id, username: user.username })
      })
    })
    passport.deserializeUser(async(user, done) => {
      let result = await db.collection('user').findOne({_id : new ObjectId(user.id) })
      delete result.password
      process.nextTick(() => {
        return done(null, result)
      })
      console.log('id:' + user.id)
    })
    app.get('/login', async function(요청, 응답) {
      console.log(요청.user)
      응답.render('login.ejs')
    })
    app.post('/login', async (요청, 응답, next) => {
      passport.authenticate('local', (error, user, info) => {
          if (error) return 응답.status(500).json(error)
          if (!user) return 응답.status(401).json(info.message)
          요청.logIn(user, (err) => {
            if (err) return next(err)
            응답.redirect('/list')
          })
      })(요청, 응답, next)
    })
    
    선생님 이렇게 썼을때 로그인 성공되어서 list 페이지로 잘 돌아가고 
    LocalStrategy와 serializeUser에서 console.log(user) 를 출력했을 때는 
    {
      _id: new ObjectId("65db3f9146b54006cc941429"),
      username: 'test',
      password: '1234'
    }
    { id: '65db3f9146b54006cc941429', username: 'test' } 등등
    이렇게 정보가 잘 출력이 되는데 
    
    /login 페이지로 접속하면 
    
    캡처
    터미널에 이렇게 undefined뜨는 이유가 뭘까요?
    
    쿠키도 잘떴습니다 
    캡처2
    
    
    몽고db도 잘 있습니다.
    캡처22
    
    
    
    #114376

    codingapple
    키 마스터
    다른 여러페이지들에서도 요청.user 나오나 출력해봅시다
    #114443

    김예지
    참가자
    다른 페이지에서도 undefined 값이 뜨네요..
    #114485

    codingapple
    키 마스터
    app.use(passport.initialize())
    app.use(session({})
    app.use(passport.session())
    이것도 위에 차례대로 잘 해놧나 확인합시다
    #114557

    김예지
    참가자
    선생님 딱히 수정한건 없는데.. 오늘은 잘 출력이 됩니다..😥
    list 접속 -----------
    { _id: new ObjectId("65dc8abe45349346d1a22c0d"), username: 'test' }
    deserializeUser---------
    { id: '65dc8abe45349346d1a22c0d', username: 'test' }
    write 접속 -----------
    { _id: new ObjectId("65dc8abe45349346d1a22c0d"), username: 'test' }
    
    
5 글 보임 - 1 에서 5 까지 (총 5 중에서)
  • 답변은 로그인 후 가능합니다.

About

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

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

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