Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用户登录laravel 5.2时如何获取api_令牌_Php_Api_Eloquent_Laravel 5.2_Restful Authentication - Fatal编程技术网

Php 用户登录laravel 5.2时如何获取api_令牌

Php 用户登录laravel 5.2时如何获取api_令牌,php,api,eloquent,laravel-5.2,restful-authentication,Php,Api,Eloquent,Laravel 5.2,Restful Authentication,我正在使用令牌保护来使用post请求中传递的api_令牌对用户进行身份验证。但我无法使用令牌保护检索api_令牌。我想从retrieveByCredentials($credentials)获取用户,并使用validateCredentials($user,$credentials)检查密码,它们位于EloquentUserProvider.php,但要访问它们,我必须使用Auth::guard('api')->provider,其中provider是受保护的属性。所以我必须手动操作。在实现登录

我正在使用令牌保护来使用post请求中传递的api_令牌对用户进行身份验证。但我无法使用令牌保护检索api_令牌。我想从
retrieveByCredentials($credentials)
获取用户,并使用
validateCredentials($user,$credentials)
检查密码,它们位于
EloquentUserProvider.php
,但要访问它们,我必须使用
Auth::guard('api')->provider
,其中provider是受保护的属性。所以我必须手动操作。在实现登录时,是否有任何简单的过程来获取api_令牌

AuthController.php中的我的登录方法

public function api_login(Request $request)
    {
        $validator = Validator::make($request->all(), ['email' => 'required', 'password' => 'required']);

        $credentials = $this->getCredentials($request);

        $user = Auth::guard('api')->provider->retrieveByCredentials($credentials);
        if(is_null($user)) {
            return response()->json([
                'error' => [
                    'message' => 'Your email is not registered yet',
                    'status_code' => 40
                ]
            ]);
        } elseif(Auth::guard('api')->provider->validateCredentials($user, $credentials)) {
            return response()->json([
                'success' => [
                    'api_token' => Auth::guard('api')->user()->api_token,
                    'message' => 'Login successful', 
                    'status_code' => 200
                ]
            ]);
        }
        return response()->json([
            'error' => [
                'message' => 'Login failed', 
                'status_code' => 20
            ]
        ]);
    }

注意:我使用了provider,但我不能使用它,因为它是一个受保护的属性。

在Laravel 5.2中,有两个身份验证保护
'web'
'api'
,它们分别在
SessionGuard
TokenGuard
中实现,如您所知

TokenGuard
没有用于检查电子邮件和密码验证的模块,它仅使用
api\u令牌检查有效性。
但是SessionGuard
<代码>尝试()方法

因此,我们只能将web guard用于登录,并且可以将
'auth:api'
中间件用于其他api

这里是登录功能,很容易实现

public function api_login(Request $request)
{
    $validator = Validator::make($request->all(), ['email' => 'required', 'password' => 'required']);

    $credentials = $this->getCredentials($request);

    // only web guard can validate with credentials
    $guard = Auth::guard('web');

    if ($guard->attempt($credentials)) {

        // maybe you can generate api_token again here

        return response()->json([
            'success' => [
                'api_token' => $guard->user()->api_token,
                'message' => 'Login successful', 
                'status_code' => 200
            ]
        ]);
    } 
    else {
        return response()->json([
            'error' => [
                'message' => 'Login failed', 
                'status_code' => 200
            ]
        ]);
    }
}