Laravel, axios: The first login request to server returns strange data {two_factor: false} instead of Auth:id(). How to fix it?


I want make axios return a response of just logged in user’s id. When I was using Laravel Fortify with blade.php files, everything was too simple. Fortify was doing redirect to home page automatically after the successful login. But when I started to use Laravel with vue js and vuex, I faced a problem.
When I send login request by axios.post I get a strange response like this enter image description here

only after the second same request I get what I need from the needed controller, where I set the redirect home value. See what I mean in below picture.

enter image description here

I found an information that the problem partly tied with AppHttpMiddlewareRedirectIfAuthenticated.php class

but I still don’t know what to do.

Here’s my axios code

 actions:
        {
            login(context,payload) {
                axios.post('/login', {
                        email: payload.email,
                        password: payload.password
                    }
                ).then(response => {
                    console.log(payload.email+" "+payload.password);
                    console.log(response.data);
                    if (response.status === 201) {
                        console.log('login')
                    } else {
                        //console.log(response.data)
                    }
                }).catch(error => {
                    console.log('oops')
                });
            },

            logout() {
                axios.post('logout').then(response => {
                    if (response.status === 302 || 401) {
                        console.log('logout')
                    }
                }).catch(error => {
                    console.log('oops')
                });
            },
        }  

And the controller code part which must work when fortify redirects after the successful login.

class UsersProfilesController extends Controller
{
    public function index()
    {
        return Auth::id();
    }
}

In my case it redirects since the second login request.
At first one if the login values are not correct, for example, not correct password, then returns response status code 422. But if username and password are correct it returns that strange code which I showed above in first screenshot.

I want to know. How can I get the redirect from the first request or if it is somehow can’t be done, then just get a status code, that login process went successful?

Source: Vue – Stack Overflow

November 28, 2021
Category : News
Tags: axios | laravel | php | Vue.js | vuex

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.