Programing

변수 값을 수정하는 방법은 다음 번 방법 리디렉션 후 항상 null임

c10106 2022. 3. 23. 20:02
반응형

변수 값을 수정하는 방법은 다음 번 방법 리디렉션 후 항상 null임

나는 vue-cli 3 패키지를 사용해 본다.
그것은 vue 버전 2.6.10이다.
라우트를 보호하기 위해 Route 전에 라우터를 사용하고 처음 로그인할 때 벌금형이 부과되지만 쿠키를 삭제할 때 경로 가드는 api에 전달할 쿠키 값을 전혀 얻지 못하며 헤더 요청 시 항상 null로 표시된다.

아래 내 코드를 확인해줘.

나는 vuex를 시도하지만 운이 없다.

import Vue from 'vue'
import Router from 'vue-router'
import Login from './views/login/route'
import Dashboard from './views/dashboard/route'
import axios from '../lib/axios'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    ...Login,
    ...Dashboard
  ]
})

router.beforeEach( async (to, from, next) => {

  // validate route is auth
  if(to.matched.some(record => record.meta.auth)){
      var cookie = $cookies.get('basic-app-token');
      if(cookie != null){
          console.log('before send', cookie);
          let res = await axios.get('http://localhost:8000/api/token/check',{
            headers: {
              Authorization: 'Bearer '+cookie
            }
          });
          console.log('after send', res.data);
          if(res.data.success){
              next()
          }else{
            next({name: 'login'})
          }
      }else{
        next({name: 'login'})
      }
  }else{
    next()
  }

})

export default router

요청 헤더의 결과 항상 "승인:베어러 null".

참조URL: https://stackoverflow.com/questions/58270586/how-to-fix-variable-value-always-be-null-after-next-method-redirect

반응형