Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

maltekiefer's avatar

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

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', 'SalutationsController@store');
});
0 likes
2 replies
MaverickChan's avatar

login?

axios.post('your/login/endpoint',{
	username: username-in-form,
	password: password-in-form
})

what is the backend auth package you using? i suggest Laravel Sanctum

gitwithravish's avatar

Is this a web route or api route? If its a web route, and have auth middleware hooked, then it should work properly.

Please or to participate in this conversation.