sourcecode

로그인 후 NuxtJ 리다이렉트

copyscript 2022. 8. 27. 10:10
반응형

로그인 후 NuxtJ 리다이렉트

Nuxtjs와 Laravel Passport를 사용하고 있습니다.사용자가 /someurl로 이동하는데 로그인 메서드가 '/'로 이동하는 경우 로그인 시 세션 변수에 푸시할 수 있도록 세션에 /someurl을 저장하는 방법은 무엇입니까?호출되는 세션 변수가 있지만 인증 미들웨어에서 새로 고쳐집니다.뭔가 놓치고 있는 게 분명해내 코드는 다음과 같습니다.auth.js미들웨어 파일

export default function ({ route, store, redirect }) {
let params = ''
if (!route.path.startsWith('/login') && !route.path.startsWith('/assets') && route.path !== '/') {
    store.state.requestUrl = route.path
}
if (!store.state.authenticated) {
    return redirect('/login')
}

및 로그인

await this.$store.dispatch('login', {
    username: this.loginDetails.username,
    password: this.loginDetails.password
})
    this.$router.push(this.$store.state.requestUrl) // this is always '/'
} catch (e) {
    console.log(e)
    this.failedLogin = true
}

이 문제를 해결하는 방법은 간단했다.login.vue내보내기가 선을 제거합니다.auth:false루팅을 무시하기 위해 사용됩니다.

변환( )을 사용하여 상태(store.state.requestUrl)를 변경해야 합니다.dispatch저장 중인 메서드 - 이미 사용 중입니다.this.$store.dispatch('login')따라서 스토어의 상태를 변경하는 돌연변이를 작성해야 합니다. this.$store.dispatch('set_request_url', requestUrl).

Nuxt :) https://auth.nuxtjs.org/에 인증용 모듈이 있습니다.그러나 직접 구현하려면 쿠키에 리다이렉트 URL을 저장합니다(vuex보다 내구성이 좋기 때문입니다).Nuxt에는 쿠키에 저장하기 위한 모듈 https://www.npmjs.com/package/cookie-universal-nuxt,이 있지만 vuejs 쿠키 모듈 https://github.com/alfhen/vue-cookie도 사용할 수 있습니다.

.
.
.


        data: () => ({

          previousPage:null,

        }),
        beforeRouteEnter(to, from, next) {
          next(vm => {
            vm.previousPage = from
          })
        },
    created(){
       console.log(this.previousPage.name);
    }
.
.
.

이거 드셔보세요

사용방법:

this.$router.push({path: "panel"})

@nuxtjs/auth 및 @nuxtjs/auth-next를 사용하는 경우 다음 설정을 nuxt.config.js 파일에 추가합니다.

auth{
redirect: {
      login: "/user/login",//this example of the path of login page in your project
      logout: "/", //this will redirect to your home after logout
      home: "/user/account" //this example will redirect to the path of user account page in your project.
    }
}

언급URL : https://stackoverflow.com/questions/51850230/nuxtjs-redirect-after-login

반응형