2016-06-30 10 views
0

私たちは、フロントエンドのlaravelとバックエンドのAPIの2つのlaravelプロジェクトに取り組んでいます。私はこの2つのプロジェクトを接続するためのチュートリアルに従いましたが、guzzlehttpを使用しています。しかし、私は未定義のインデックスパスワードを取得しています。私はすでにgetUsersメソッドでユーザー['data']をddし、正しいパスワードを取得します。誰も私にこれを手伝うことができますか?別のapiに接続するカスタム認証でログインできません

ApiUserProvider 

<?php 

namespace App\Auth; 

use Illuminate\Contracts\Auth\UserProvider; 
use Illuminate\Contracts\Auth\Authenticatable as UserContract; 
use Illuminate\Http\Request; 

use GuzzleHttp\Client; 

class ApiUserProvider implements UserProvider 
{ 

    public function retrieveByCredentials(array $credentials) 
    { 
     $user = $this->getUserByUsername($credentials['username']); 

     return $this->getApiUser($user); 
    } 

    public function retrieveById($identifier) 
    { 
     $user = $this->getUserById($identifier); 

     return $this->getApiUser($user); 
    } 

    public function validateCredentials(UserContract $user, array $credentials) 
    { 
     return $user->getAuthPassword() == bcrypt($credentials['password']); 
    } 

    protected function getApiUser($user) 
    { 
     if ($user !== null) { 
      return new ApiUser($user); 
     } 
    } 

    protected function getUsers() 
    { 
     $client = new Client(['base_uri' => 'http://127.0.0.1:80/api.kourse/public/api/v1/']); 

     $response1 = $client->request('POST', 'oauth/access_token', [ 
      'form_params' => [ 
       'client_id' => 'id1', 
       'client_secret' => 'secret1', 
       'grant_type' => 'password', 
       'username' => '[email protected]', 
       'password' => 'password' 
      ] 
     ]); 


     $location = json_decode($response1->getBody(), true); 

     $token = $location['access_token']; 

     // Send a request to https://foo.com/api/test 
     $response2 = $client->request('GET', 'users/self', [ 
      'headers' => [ 
       'Authorization' => 'Bearer '. $token 
      ] 
     ]); 

     $user = json_decode($response2->getBody(), true); 
     return $user['data']; 
    } 

    protected function getUserById($id) 
    { 
     $user = []; 

     if($this->getUsers()['email'] == $id){ 
      $user['id'] = $id; 
     } 

     dd($user); 
     return $user ?: null; 
    } 

    protected function getUserByUsername($username) 
    { 
     $user = []; 


     if($this->getUsers()['email'] == $username){ 
      $user['email'] = $username; 
     } 

     return $user ?: null; 
    } 

    // The methods below need to be defined because of the Authenticatable contract 
    // but need no implementation for 'Auth::attempt' to work and can be implemented 
    // if you need their functionality 
    public function retrieveByToken($identifier, $token) { } 
    public function updateRememberToken(UserContract $user, $token) { } 

} 

ApiUser

namespace App\Auth; 

use Illuminate\Auth\GenericUser; 
use Illuminate\Contracts\Auth\Authenticatable as UserContract; 

class ApiUser extends GenericUser implements UserContract 
{ 


    public function getAuthIdentifier() 
    { 
     return $this->attributes['id']; 
    } 
} 

UserControllerで

public function login(Request $request) 
{ 
    $email = $request->email; 
    $password = bcrypt($request->password); 

    if (Auth::attempt(['username' => $email, 'password' => $password])) { 
     return "hello"; 
    } 
} 

エラー enter image description here

AuthServiceProvider

class AuthServiceProvider extends ServiceProvider 
{ 
    /** 
    * The policy mappings for the application. 
    * 
    * @var array 
    */ 
    protected $policies = [ 
     'App\Model' => 'App\Policies\ModelPolicy', 
    ]; 

    /** 
    * Register any application authentication/authorization services. 
    * 
    * @param \Illuminate\Contracts\Auth\Access\Gate $gate 
    * @return void 
    */ 
    public function boot(GateContract $gate) 
    { 
     $this->registerPolicies($gate); 

     Auth::provider('api', function($app, array $config) { 
      return new ApiUserProvider($config['model']); 
     }); 
    } 
} 
+0

ログから完全なエラーを貼り付け、いつ受け取ったかを説明できますか? – TheFallen

+0

GenericUser.phpのErrorException 56: 未定義のインデックス:パスワード。ログイン時にエラーが発生します。ApiUserProvider validateCredentialsでエラーが発生しました – Jearson

+0

@TheFallen – Jearson

答えて

0

私の最高の推測では、Userモデルを開くことであろうと、あなたが持っている場合:

protected $hidden = [ 
    'password', 
    'remember_token', 
]; 

はこのprotected $hidden = [];ような空の配列、作ること。あなたが新しいApiUserを作成するときに、Userオブジェクトを配列に変換し、$ hiddenプロパティのためにパスワード属性を削除するので、これはうまくいくと思います。

+0

ありがとうございました。今のところ私は検証資格をtrueに戻しました。今私はApiUser.php行でこのErrorExceptionを取得しています:未定義のインデックス:ID。 ApiUserProviderを正しい方法で登録したかどうか確認してください。最新の質問をご覧ください – Jearson

関連する問題