2016-03-21 4 views
0

Ich habe in einer Laravel 5.1 App eine API-Sektion erstellt, in der ich JWT auth für staatenlose Anmeldung und Validierung verwende. Die App verwendet den von laravel bereitgestellten Auth-Dienst und die Tabelle 'users' als Standard. Meine API benötigt eine Authentifizierung in der 'clients'-Tabelle. Ich habe es geschafft, umgehen die Benutzer-Tabelle bei der Verwendung von JWT, indem Sie eine Middleware, die die Auth.php Konfigurationsdatei auf model => 'Models\AuthClient' und table => 'clients' ändert. Alles gut, Validierung funktioniert, es erstellt das Token, wenn die Anmeldeinformationen korrekt sind.Laravel 5.1 JWT Holen Sie sich den eingeloggten Benutzer aus der benutzerdefinierten Tabelle

Middleware:

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

    return $next($request); 
} 

ApiAuthController Login-Funktion:

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

Mein Problem ist, wenn ich versuche, den angemeldeten Benutzer aus dem Token wie folgt abzurufen:

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

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

Die authenticate() function gibt ein Match-Modell aus der Tabelle 'users' anstelle von 'customers' alt zurück obwohl ich die auth.php und jwt.php auf 'Models \ AuthClient' gesetzt habe und die ID von 'clients' stammt.

AuthCient Modell:

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

Was bin ich?

Danke!

Antwort

2

Ich habe die Middleware in die Hauptroutengruppe (api-amanet) gelegt und ich denke, das hat den Trick gemacht. Bevor ich die auth.php in der Authentifizierungsfunktion änderte, vermute ich, dass die Auth-Klasse bereits mit den standardmäßigen Einstellungen von auth.php instanziiert wurde und nicht "aktualisiert" wurde, wenn ich die Einstellungen änderte.

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

Route::post('authenticate','Api\[email protected]'); 

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

    Route::resource('update-profile','Api\[email protected]'); 

    Route::resource('get-contracts','Api\[email protected]'); 
}); 

});

Hoffe das hilft jemand anderem.