简体   繁体   English

nuxt auth - 登录:false 和用户:null

[英]nuxt auth - loggedIn:false and user:null

pls can someone help me, i use Nuxt js and laravel.请有人帮助我,我使用 Nuxt js 和 laravel。 The user logged in successfully but the loggedIn is still false and and user is null, i have tried everything what else can i do pls help me.用户成功登录,但登录仍然是假的,用户是 null,我已经尝试了所有其他方法,请帮助我。 I think the error is from the state我认为错误来自 state

nuxt.config.js nuxt.config.js

export default {
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: 'laravel-nuxt-api',
    htmlAttrs: {
      lang: 'en'
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' },
      
      { name: 'format-detection', content: 'telephone=no' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [
  ],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
  ],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/tailwindcss
    '@nuxtjs/tailwindcss',
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
  '@nuxtjs/axios',
   '@nuxtjs/auth-next',
  ],
  auth:{
   strategies: {
    laravelSanctum: {
      provider: 'laravel/sanctum',
      url: 'http://127.0.0.1:8000', 
     
      user: {
                property:false,
            }, 
      endpoints:{
          login:{url:"/api/login",method:"post"},
          logout:{url:"/api/logout",method:"post"},
          allrecords:{url:"/api/allrecords",method:"post"},
          user: {url: "/api/user",method:"get", property: false }
        },
        
       }
      
      },
      redirect:{
        login:'/auth/login',
        logout:'/',
        home:'/'
      }
    },
  axios:{
    baseURL:"http://127.0.0.1:8000",
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
  }
}

login.vue登录.vue

<template>
    <div>
        <Navbar/>
        <a href="#" @click="login()">Login</a>

    </div>

</template>
<script>
    export default {
        data(){
            return {
                form:{
                    email: 'ade@gmail.com',
                     password: '12345'
                }
            }
        },
        methods:{
               async login(){
           this.errors='';
           try {
               const res = await this.$auth.loginWith('laravelSanctum', {
          data:this.form})
               if(res){
                await this.$auth.setUser(res.data.user )
                console.log(res);
               }
            //    console.log(res);
           } catch (error) {
               console.log(error)

           }


        }
        }
    }
</script>

Package.json Package.json

{
  "name": "laravel-nuxt-api",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "nuxt",
    "build": "nuxt build",
    "start": "nuxt start",
    "generate": "nuxt generate"
  },
  "dependencies": {
    "@nuxtjs/auth": "^4.9.1",
    "@nuxtjs/auth-next": "5.0.0-1637745161.ea53f98",
    "@nuxtjs/axios": "^5.13.6",
    "core-js": "^3.15.1",
    "nuxt": "^2.15.7"
  },
  "devDependencies": {
    "@nuxtjs/tailwindcss": "^4.2.0",
    "postcss": "^8.3.5"
  }
}

it's working fine in post man please help me它在邮递员中工作正常,请帮助我

看图

(Vuex状态)用户:仍然是Null

Setting the following in the nuxt.config.js filenuxt.config.js文件中设置以下内容

auth: {
  strategies: {
    laravelSanctum: {
      user: {
        property: false,
        autoFetch: false,
      },
    },
  },
},

and calling并打电话

await this.$auth.loginWith('laravelSanctum', ...)
await this.$auth.setUser({ ... }) // your cool JS object representing the user

should do the job here.应该在这里完成工作。

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

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