在laravel / fortify

问题描述

我正在使用laravel / fortify,并且尝试使用HTTP客户端请求进行身份验证

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;
        }

});

但是我得到了错误,错误是:

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

解决方法

像这样返回用户实例

 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);
            }

或者您像官方文件所说的那样

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

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

参考链接https://jetstream.laravel.com/1.x/features/authentication.html

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...