maltekiefer
2 months ago
242
2
Vue

Get username on Vue.js Ajax post request / Auth with the API

Posted 2 months ago by maltekiefer

Hello,

I have the create method in my controller. There I have the following so far:

$salutation->created_by = Auth::user()->name;

But now it does not work if I make the call with Vue.js:

    onSubmitSalutation(evt) {
        evt.preventDefault()
        this.errors = {};
        axios.post('/api/postSalutation', {
            description: this.description
        }).then(response => {
            this.$bvModal.hide('createSalutation');
            this.getSalutations();
        }).catch(error => {
            if (error.response.status === 422) {
                this.errors = error.response.data.errors || {};
            }
        });
    },

So my question is, how can I send the username, or even better authenticate myself for the API, so that I can use the middelware:

Route::group(['middleware' => ['auth']], function () {
    Route::post('/postSalutation', '[email protected]');
});

Please sign in or create an account to participate in this conversation.