0

I have a modal where I can create/edit user. But when I click on the submit button I got a 401 error.

The method looks like this:


sendUserData(){
                axios.post('/api/saveUser', {
                    headers: {
                        Authorization: 'Bearer ' + localStorage.getItem('token')
                    },
                    id: this.id,
                    name: this.name,
                    email: this.email,
                    password: this.password
                }).then(response => {
                    console.log(response.data);
                    this.$emit('userSaved')
                }).catch(error => {
                    console.log(error)
                });
            }

The api.php looks like this:


Route::group([

    'middleware' => 'api',
    'prefix' => 'auth'

], function ($router) {

    Route::post('login', 'AuthController@login');
    Route::post('logout', 'AuthController@logout');
    Route::post('refresh', 'AuthController@refresh');

});

Route::middleware('auth:api')->group(function () {
    Route::post('saveUser', 'UserController@saveUser');
});

If I console.log the token I got a valid JSON token

How can I fix this?

1
  • Did you try to use web.php instead of api.php ? Maybe it's the first step to solve your issue Commented Sep 3, 2019 at 13:11

1 Answer 1

3

Your axios function is not well formatted. It should be like this:

axios.post('/api/saveUser', {
    id: this.id,
    name: this.name,
    email: this.email,
    password: this.password
}, {
  headers: {
    Authorization: 'Bearer ' + localStorage.getItem('token')
  },
}).then(response => {
    console.log(response.data);
    this.$emit('userSaved')
}).catch(error => {
    console.log(error)
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.