当您从laravel 5.6中的表单发送数据时,为什么会出现错误401?

时间:2018-11-07 01:44:46

标签: php authentication axios laravel-5.6

我工作laravel 5.6.23和vue.js,我用自己的守护者(admin)实现了中间件auth。这些是我的路线。

Route::prefix('admin')->middleware('auth:admin')->group(function () {
    Route::get('/', function () {
        return view('admin.welcomeLibrarian');
    });
    Route::resource('Admins','AdminController');
    Route::resource('Authors', 'AuthorController');
    Route::get('Books/authors','BookController@authors');
    Route::get('Books/themes','BookController@themes');
    Route::get('Books/editorials','BookController@editorials');
    Route::get('Books/check/{id}','BookController@check');
    Route::resource('Books', 'BookController');
    Route::get('Loans/debtsUser/{id}','LoanController@debtsUser');
    Route::get('Loans/userLoads/{id}','LoanController@userLoads');
    Route::get('Loans/destroyDebt/{id}','LoanController@destroyDebt');
    Route::resource('Loans', 'LoanController');
    Route::resource('Editorials', 'EditorialController');
    Route::resource('Themes', 'ThemesController');
    Route::post('RegisterUser','Auth\RegisterController@create');

    Route::get('Users','Auth\UserController@listUsers');
    Route::delete('Users/{id}','Auth\UserController@destroy');
    Route::put('Users/{id}','Auth\UserController@update');
    Route::get('userShow/{id}','Auth\UserController@show');
});

当您从表单发送数据时,一切正常,但是从用户表单发送数据时出现错误401,我不明白为什么只有创建新用户的路由失败了?下一个模板是一个插槽。

<template>
    <v-flex xs12 sm12 lg6>
    <v-card>
        <v-toolbar color="pink">
            <v-card-title primary-title>{{ source.title }}</v-card-title>
        </v-toolbar>
        <v-form v-model="valid" >
            <input type="hidden" name="_token" :value="csrf">
            <slot name='content-form'></slot>
        </v-form>
        <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn color="green darken-1" flat="flat" @click="save()"  :disabled="!valid">Save</v-btn>
            <v-btn color="green darken-1" flat="flat" @click="$router.push(source.return)">Cancel</v-btn>          
        </v-card-actions>
    </v-card>
    <v-dialog v-model="dialog" max-width="290">
      <v-card>
        <v-card-title class="headline">{{ msm }}</v-card-title>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="green darken-1" flat="flat"  @click="$router.push(source.return)">return to list</v-btn>
          <v-btn color="green darken-1" flat="flat"  @click="resetForm">continue here</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
    </v-flex>
</template>
<script>
export default {
    props:{
        source:Object,
        form:Object
    },
    data: () => ({
        csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
        valid: true,
        dialog:false,
        msm:'',
        error:false
    }),
    methods:{
        save(){
                let vm=this
                axios[this.source.method](this.source.create,this.form)
                    .then(function(response) {
                        Vue.set(vm.$data,'msm',response.data)
                    })
                    .catch(function(error) {
                        Vue.set(vm.$data, 'msm', error.response)
                        Vue.set(vm.$data,'error',true)
                })
                this.dialog=true
            },
        resetForm(){
            this.$emit('resetProps')
            this.dialog=false
        }
    } 
}
</script>

这是给用户的

<template>
  <v-layout justify-center align-center >
    <form-component :source='source' :form='form'>
        <div slot='content-form'>
            <v-text-field v-model="form.id" :rules="idRules" label="ID" v-show='updateShow' required></v-text-field>
            <v-text-field v-model='form.name' :rules="nameRules" label="Name" required></v-text-field>
            <v-text-field v-model="form.email" :rules="emailRules" label="email" required></v-text-field>
            <v-text-field v-model="form.phone" :rules="idRules" label="phone" required></v-text-field>
            <v-text-field v-model="form.password"  label="password" type="password" :rules="passRule" ></v-text-field>
        </div>
    </form-component>
  </v-layout>
</template>
<script>
import formComponent from "../utilities/form.vue"
export default {
    data: () => ({
        updateShow:true,
        source:{
            title:'Add User',
            create:'admin/RegisterUser',
            method:'post',
            return:'/Users'
        },
        form:{
            id:'',
            name:'',
            phone:'',
            password:'',

        },
        user:{},
        items: ['user','librarian','administrador'],
        nameRules:[
                v=> /^[A-Za-z\s]*$/gi.test(v) || 'sorry, the only letters ',
                v=> !!v || 'sorry, the name is required'
        ],
        idRules:[
                v=> /^[0-9]*$/gi.test(v) || 'sorry, the only numbers ',
                v=> !!v || 'sorry, the id is required'
        ],
        emailRules:[
            v => !!v || 'E-mail is required',
            v => /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(v) || 'E-mail must be valid'
        ],
        passRule:[v => !!v || 'the password is required']
    }),
    beforeMount(){
        if(this.$route.meta.mode === 'edit') {
            this.source.title = 'Edit User'
            this.source.create = 'admin/Users/' + this.$route.params.id
            this.source.method = 'put'
            this.initializeUpdate(this.$route.params.id)
            this.updateShow=false
        }
    },
    components:{
        formComponent
    },
    methods:{
        initializeUpdate(id){
            let vm=this
            axios.get(`admin/userShow/${id}`)
            .then(function(response) {
                Vue.set(vm.$data,'form',response.data)
            })
        }
    }
}
</script>

1 个答案:

答案 0 :(得分:0)

错误出在控制器中,我忘了为多个身份验证进行正确的配置。 之前:

public function __construct()
{
    $this->middleware('guest');
}

之后:

public function __construct()
{
    $this->middleware('guest:web');
}