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

home2 게시판 Node.js, Express 게시판 로그인한 유저 정보 유지

로그인한 유저 정보 유지

10 글 보임 - 1 에서 10 까지 (총 13 중에서)
  • 글쓴이
  • #43651

    유용환
    참가자
    강의에서 대로 로컬스토리지에 있던것을 꺼내서 보여주려고
    this.$store.state.new_us = this.$store.commit('exuid', JSON.parse(localStorage.getItem('user')))
    이렇게 했는데 출력이 안되서 콘솔로 확인을 해보았더니 undefined 이렇게 나오는데 왜 정의가 되지 않은건가요
    
     onAuthStateChanged 위에 했고 
    exuid(state, payload){
          state.outuid = payload
        }   => store.js
    
    outuid = ''  이렇게 하고 => store.js
    
    
    this.$store.state.new_us = this.$store.commit('exuid', JSON.parse(localStorage.getItem('user')))
    
    firebase.auth().onAuthStateChanged((user)=>{
        if (user) {
          this.$store.commit('new_users', user.uid);
          if (this.$store.state.new_us.length > 4){
            this.$store.state.new_us = this.$store.state.new_us.substr(0, 4)
            localStorage.setItem('user', JSON.stringify((this.$store.state.new_us)))
          }
          
        }
        
      })
    
    이렇게 했습니다 . this.$store.state.new_us이 정의가 안된것 같은데 왜 그런지 모르겠습니다.
    로컬스토리지에는 잘 나옵니다.
    
    
    
    
    
    #43684

    codingapple
    키 마스터
    문자나 숫자는 JSON.parse JSON.stringify에 넣을 필요 없습니다
    this.$store.state.new_us = this.$store.state.new_us.substr(0, 4)
    state를 직접수정하기보다는 state수정함수를 만들어사용합시다 
    
    
    #43698

    유용환
    참가자
    state를 직접 수정해서 다시 새로고침 했을때 로그인한 유저의 정보가 유지 안되는건가요 JSON.parse JSON.stringify을
    빼고 실행시켜도 안되네요
    #43765

    codingapple
    키 마스터
    새로고침하면 state는 초기화됩니다 
    새로고침시 로컬스토리지에 유저정보가 있으면 그거 store에 넣으라고 코드짜면 될듯요
    #43799

    유용환
    참가자
    if (this.$router.go()){
          if(localStorage.getItem('user') != null){
            this.$store.state.new_us = localStorage.getItem('user')
          }
        }
    
    이렇게 해봤는데 실행하면 로그인을 했을때 아무런 반응이 없는데 위 코드에서 어떤것이 문제인가요
    #43830

    codingapple
    키 마스터
    mounted 이런데다 써도 됩니다 
    state수정은 state수정함수를 씁시다
    #43834

    유용환
    참가자
    mounted() {
        if(localStorage.getItem('user') != null){
            this.$store.commit('exuid', localStorage.getItem('user'))
          }
      },
    
    => store.js
    
    outuid : ''
    exuid(state, payload){
          state.outuid = payload
          state.new_us = state.outuid
        },
    
    결과를 보여줄때는 $store.state.new_us 사용합니다.
    #43919

    codingapple
    키 마스터
    이제 로그인시 로컬스토리지에 유저정보 저장하는 코드만 잘 만들면 되겠군요
    #43971

    유용환
    참가자
    localStorage.setItem('user', this.$store.state.new_us) 이것도 추가했는데 새로고침하면 로컬스토리지에는 남아있는데 페이지로 
    볼때는 여전히 로그인이 안한상태가 됩니다.
    직접 코드에 넣어보아서 왜 그런지 확인해 주실수있나요
    
    
    #43999

    codingapple
    키 마스터
    state와 로컬스토리지에 잘 저장되어있는데 페이지에서만 안나오는거면
    페이지에 그것들을 출력하는 문법을 살펴보면 됩니다
10 글 보임 - 1 에서 10 까지 (총 13 중에서)
  • 답변은 로그인 후 가능합니다.

About

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

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

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