无法使用连接到另一个api的自定义身份验证登录

时间:2016-06-30 07:20:35

标签: laravel laravel-5 laravel-5.1 laravel-5.2 guzzle

我们正在开展两个laravel项目,一个用于前端laravel和后端api。我按照关于连接这两个项目的教程,但使用guzzlehttp。但是我得到了未定义的索引密码。我已经在getUsers方法中找到了用户['data']并获取了正确的密码。谁可以帮我这个事。

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@yahoo',
                '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']);
        });
    }
}

1 个答案:

答案 0 :(得分:0)

我最好的猜测是打开用户模型,如果你有:

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

使它成为一个空数组,如protected $hidden = [];。我想这可能会有效,因为当您创建新的ApiUser return new ApiUser($user);时,它会将User对象转换为数组,并且由于$ hidden属性而删除了password属性。