jeudi 30 juin 2016

Cannot login with custom authentication connecting to another api

We are working on two laravel projects one for front end laravel and for backend api. I followed tutorials on connecting this two projects but make use of guzzlehttp. However I am getting undefined index password. I already dd the user['data'] in getUsers method and gettign the correct password. Can any one help me on this.

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://ift.tt/294zhj7']);

        $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 http://ift.tt/295jtJe
        $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";
    } 
}

error enter image description here



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2921AKs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire