laravel-fortify

How to get user information after login in laravel fortify


I am using laravel fortify in my single page vue application. When i send XHR request to sitePath + '/login' i get {two_factor: false} in response and user is logged in. How can i add user information so i can save the data in local storage.

let response = await this.$root.requestPost(data, url);
async requestPost(data, requestUrl) {
    const response = await axios.post(requestUrl, {
        // ...data
    }).catch(error => {
        //
    });
    return response;
},

Solution

  • Sorry for late answer.

    But today I have solved same problem.

    In FortifyServiceProvider's register() method, you need implement own LoginResponse:

    $this->app->instance(LoginResponse::class, new class implements LoginResponse {
            public function toResponse($request)
            {
                /**
                 * @var User $user
                 */
                $user = $request->user();
                return $request->wantsJson()
                    ? response()->json(['two_factor' => false, 'email' => $user->email ])
                    : redirect()->intended(Fortify::redirects('login'));
            }
        });
    

    Documentation - https://laravel.com/docs/8.x/fortify#customizing-authentication-redirects