Kod*_*kSA 6 vue.js vuejs2 nuxt.js
我正在使用 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')
}
Run Code Online (Sandbox Code Playgroud)
并登录
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
}
Run Code Online (Sandbox Code Playgroud)
dispatch您需要使用突变( store 中的方法 - 您已经在 中使用过它)来更改状态(store.state.requestUrl)this.$store.dispatch('login')。因此,您需要编写一个突变来改变商店中的状态,例如。this.$store.dispatch('set_request_url', requestUrl)。
顺便说一句:Nuxt 中有一个用于身份验证的模块:) https://auth.nuxtjs.org/ 我建议使用它!但如果我想自己实现它,我会在 cookies 中存储一个重定向 url(因为它比 vuex 更耐用)。Nuxt 有一个用于存储 cookie 的模块:https://www.npmjs.com/package/cookie-universal-nuxt,但您也可以使用 vuejs cookies 模块: https: //github.com/alfhen/vue-cookie
| 归档时间: |
|
| 查看次数: |
18526 次 |
| 最近记录: |