Laravel 5.1 JWT从自定义表

时间:2016-03-21 11:03:43

标签: php laravel authentication laravel-5.1 jwt

我在Laravel 5.1应用程序内创建了一个API部分,我使用JWT auth进行无状态登录和验证。 该应用程序使用laravel和'用户提供的Auth服务。表默认。我的API需要在'客户端上进行身份验证。表。 通过制作将auth.php配置文件更改为model => 'Models\AuthClient'table => 'clients'的中间件,我设法在使用JWT时解决了users表。所有好的,验证都有效,它会在凭据正确时创建令牌。

中间件:

public function handle($request, Closure $next)
{
    \Config::set('auth.model', 'App\Models\AuthClient');
    \Config::set('auth.table', 'clients');

    return $next($request);
}

ApiAuthController登录功能:

public function authenticate(Request $request)
{
    $cred = $request->only('email', 'password', 'client_code' );

    $validator = $this->validator($cred);
    if($validator->fails()) {
        return response()->json($validator->errors());
    }

    $credentials = ['email'=> $cred['email'], 'password'=> $cred['password']];

    /*
     * If the user enters a "client_code", login the user with that credential
     */
    if(issetNotEmpty($cred['client_code'])) {
        \App\Models\AuthClient::$defaultAuth = 'client_code';
        $credentials = ['client_code' => $cred['client_code'], 'password' => $cred['client_code']];
    }

    try {
        if (!$token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'Datele de autentificare nu sunt corecte.'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    // if no errors are encountered we can return a JWT
    return response()->json(compact('token'));
}

我的问题是当我尝试从令牌中检索已记录的用户时:

public function getContracts(Request $request)
{
    $client = JWTAuth::parseToken()->authenticate();
    $contracts = $client->contracts;
    dd($client);

    return response()->json($contracts);
}

authenticate()函数会从'用户'中返回匹配模型。表而不是'客户'虽然我已将auth.php和jwt.php设置为' Models \ AuthClient' ID来自' clients'。

AuthCient模型:

    class AuthClient extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    protected $table = 'clients';

    public static $defaultAuth = 'email';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [ 'email', 'login_password', 'api_token'];

    protected $hidden = ['login_password', 'api_token'];
}

我错过了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

我已将中间件放在主路由组(api-amanet)中,我认为这样做了。 在我更改身份验证函数中的auth.php之前,我猜测Auth类已经使用默认的auth.php设置进行了实例化,并且没有"刷新"当我改变设置。

/** ********************* API ROUTES ********************** */
Route::group(['prefix' => 'api-amanet', 'middleware' => ['config.clients']], function()
{

Route::post('authenticate','Api\ApiAuthController@authenticate');

Route::group(['middleware' => ['jwt.auth']], function () {
    Route::post('password/reset', 'Api\ApiAuthController@resetPassword');

    Route::resource('update-profile','Api\ApiAuthController@updateClientProfile');

    Route::resource('get-contracts','Api\ResourceController@getContracts');
});

});

希望这有助于其他人。