Skip to content
Advertisement

Authentication using api token in laravel/fortify

I am using laravel/fortify and I was trying to authenticate using HTTP client request

public function boot()
{
    Fortify::loginView(function () {
        return view('auth.login');
    });
    Fortify::authenticateUsing(function (Request $request) {
        $response = Http::withHeaders([
            'content-type' => 'application/json',
            'Accept' =>'application/json',
        ])->post('http://127.0.0.1:8001/v1/login', [
            'email' => $request->email,
            'password' => $request->password
        ]);
        if($response->ok()) {
            //Session::put('token',$response['access_token']);
            $data = $response->json();
            return $data;
        }

});

But I am getting error and error is:

TypeError
Argument 1 passed to IlluminateAuthSessionGuard::login() must be an instance of 
IlluminateContractsAuthAuthenticatable, array given, called in 
home/suraj/Documents/locaxapp/client/locux/source/clients/vendor/laravel/fortify/src/
Actions/AttemptToAuthenticate.php on line 77

Advertisement

Answer

return user instance like this

 Fortify::authenticateUsing(function (Request $request) {
            $response = Http::withHeaders([
                'content-type' => 'application/json',
                'Accept' =>'application/json',
            ])->post('http://127.0.0.1:8001/v1/login', [
                'email' => $request->email,
                'password' => $request->password
            ]);
            if($response->ok()) {
                //Session::put('token',$response['access_token']);
                $data = $response->json();
                return User::find($data->id);
            }

or you as official doc says

Fortify::authenticateUsing(function (Request $request) {
    $user = User::where('email', $request->email)->first();

    if ($user &&
        Hash::check($request->password, $user->password)) {
        return $user;
    }
})

ref link https://jetstream.laravel.com/1.x/features/authentication.html

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement