简体   繁体   English

*Nuxt JS Auth* 为什么在模板部分中使用 loginWith 登录成功后 auth.loggedIn 为真,但在中间件文件中为假?

[英]*Nuxt JS Auth* Why after success login with loginWith in template sections auth.loggedIn is true, but in middleware file is false?

I just started learning NuxtJS and I want to implement authentication by auth module for Nuxt.我刚开始学习 NuxtJS,我想通过 auth 模块为 Nuxt 实现身份验证。 I don't know why, but login with loginWith seems working, on vue files I can get data about logged user and loggedIn variable has true value.我不知道为什么,但使用 loginWith 登录似乎有效,在 vue 文件上我可以获得有关登录用户的数据,并且 logined 变量具有真值。 But I noticed when I set middleware from auth module, even if I'm logged in, server is redirecting always to login endpoint.但是我注意到当我从 auth 模块设置中间件时,即使我已登录,服务器也总是重定向到登录端点。 I saw that I can make my middleware file, but there loggedIn has value false, so I can't be redirected correctly.我看到我可以制作我的中间件文件,但是 login 的值为 false,所以我无法正确重定向。

index.vue索引.vue

<template>
    <div id="main-page">
        <panel />
    </div>
</template>

<script>
import panel from "~/components/Panel.vue"


export default {
    middleware: ['authenticated'],
    'components': {
        panel
    },

};
</script>

My login component:我的登录组件:

<template>
    <div id="form-wrapper">
        <b-form-group 
            class="ml-3 mr-5"
            label="Username"
            label-for="id-username"
            :invalid-feedback="invalidFeedback"
            :state='state'
        >
            <b-form-input id="id-username" v-model="username" type="text" placeholder="elo"></b-form-input>        
        </b-form-group>
        <b-form-group 
            class="ml-3 mr-5"
            label="Password"
            label-for="id-password"
            :invalid-feedback="invalidFeedbackPass"
            :state='statePass'
        >
            <b-form-input id="id-password" v-model="password" type="password"></b-form-input>        
        </b-form-group>
        <b-button v-b-modal.login-modal class="ml-3 mr-5" variant="outline-success">Login</b-button>
<div v-if="$auth.loggedIn">{{ $auth.loggedIn }}</div> // here is true after login

        <b-modal ref="login-modal" id="login-modal" hide-footer hide-header>
            <p>Login: {{ username }} Hasło: {{ password }}</p>
            <b-button @click="loginUser({username, password})">Send</b-button>
        </b-modal>
    </div>    
</template>

<script>
import "bootstrap-vue"
import axios from "axios"

export default {
    name: "loginForm",
    data() {
        return{
            username: '',
            password: '',
        }
    },
    props: {

    },
    methods: {
        async loginUser(loginInfo){
            try {
                await this.$auth.loginWith('local', {
                    data: loginInfo
                });
            } catch (e) {
                this.$router.push('/');
            }
        }

    },
...
</script>

nuxt.config.js for auth用于身份验证的 nuxt.config.js

auth: {
    strategies: {
      local: {
        endpoints: {
          login: { url: '/auth/token/login/', method: 'post', propertyName: 'auth_token' },
          logout: { url: '/auth/token/logout/', method: 'post' },
          user: { url: '/auth/users/me/', method: 'get', propertyName: false }
        },
        tokenRequired: true,
        tokenType: 'Token',
        tokenName: 'Authorization'
      }
    },

    redirect: {
      login: "/",
      logout: "/",
      home: "/home"
    },

  },

autenticated.js in middleware中间件中的 authenticated.js

export default function ({ store, redirect }) {
  console.log(store.state.auth.loggedIn); // here returns false, even after success login
  if (store.state.auth.loggedIn) {
    return redirect('/home')
  }
}

Worth to mention is that, it's running on docker.值得一提的是,它在 docker 上运行。 Maybe that's a problem.也许这是一个问题。

UPDATE I changed nuxt mode from SSR to SPA and now everything works from nuxt auth module.更新我将 nuxt 模式从 SSR 更改为 SPA,现在一切都可以从 nuxt auth 模块运行。

But I want to that works on SSR, so if someone have solution, please share.但我想在 SSR 上工作,所以如果有人有解决方案,请分享。

Ok I couldn't find better solution for this, than changing nuxt's mode to SPA.好吧,我找不到比将 nuxt 的模式更改为 SPA 更好的解决方案。 In SPA everything works, how it should be.在 SPA 中,一切正常,应该如何。 Here my nuxt.conf.js fragment for auth module.这是我的 auth 模块的 nuxt.conf.js 片段。

export default{
  mode: 'spa',

  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/auth'
  ],

  axios: {
    baseURL: 'http://127.0.0.1:8000/api',
    browserBaseURL: 'http://127.0.0.1:8000/api'
  },

  auth: {

    strategies: {
      local: {
        endpoints: {
          login: { url: '/auth/token/login/', method: 'post', propertyName: 'auth_token' },
          logout: { url: '/auth/token/logout/', method: 'post' },
          user: { url: '/auth/users/me/', method: 'get', propertyName: false }
        },
        tokenRequired: true,
        tokenType: 'Token',
        tokenName: 'Authorization'
      }
    },
    rewriteRedirects: false,

    redirect: {
      login: "/login",
      logout: "/login",
      home: "/",
    },
  },

  router : {
    middleware: ['auth'],
  }
}

my nuxt.config.js also:我的 nuxt.config.js 也是:

auth: {
 strategies: {
  local: {
    endpoints: {
      login: { url: '/api/login', method: 'post' },
      logout: false,
      user: false,
    },
    tokenRequired: true,
  },
},
rewriteRedirects: false,

 redirect: {
  login: '/login',
  logout: '/login',
  home: '/dashboard',
},}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM