【问题标题】:Authentication using api token in laravel/fortify在 laravel/fortify 中使用 api 令牌进行身份验证
【发布时间】:2021-01-07 11:33:42
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: php laravel httpclient fortify laravel-8


    【解决方案1】:

    像这样返回用户实例

     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

    【讨论】:

    • @sunfarmahulla 试试我添加的解决方案
    猜你喜欢
    • 2014-09-26
    • 2014-06-08
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    相关资源
    最近更新 更多