Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Passport Register the user credentials were incorrect

I set up Laravel Passport and currently I am trying to register user with a Post Route. I did create a RegisterController inside Controllers/Api/Auth.

Thus I created a clients table which looks excatly like a users table. The client gets created if I call the route, but I do not get an access token nor a refresh token.

The route to my controller looks like this (routes/api):

Route::post('register', ['as' => 'register', 'uses' => 'Api\Auth\RegisterController@register']);

My Controller looks like this:

<?php

namespace App\Http\Controllers\Api\Auth;


use App\Client;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use Laravel\Passport\Client as PClient;

use Illuminate\Http\Request;

class RegisterController extends Controller
{
    private $client;

    public function __construct() {
        $this->client = PClient::find(1);
    }

    public function register(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|min:6|confirmed'
        ]);

        $client_user = Client::create([
            'name' => request('name'),
            'email' => request('email'),
            'password' => bcrypt(request('password'))
        ]);

        $params = [
            'grant_type' => 'password',
            'client_id' => $this->client->id,
            'client_secret' => $this->client->secret,
            'username' => request('email'),
            'password' => request('password'),
            'scope' => '*'
        ];

        $request->request->add($params);
        $proxy = Request::create('oauth/token', 'POST');
        return Route::dispatch($proxy);
    }
}

This is my Client Model:

class Client extends Model implements AuthenticatableContract,
                                      AuthorizableContract,
                                      CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword, HasApiTokens, Notifiable;

    protected $table = 'clients';

    protected $fillable = ['name', 'email', 'password'];

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

When I am trying to call it with Postman I get this error message: enter image description here

like image 983
utdev Avatar asked Jul 18 '17 15:07

utdev


2 Answers

I may be way off basis here but it looks as if you are creating your client with a password of "password" due to your bcrypt('password') call.

Should it not be bcrypt(request('password'))?

This would explain why your credentials are wrong in your request, because they are ; )

like image 178
Ryan Avatar answered Sep 21 '22 23:09

Ryan


Ok I fixed it, the post route worked if I used the User Model instead of my Client model, so I guessed that there has to be something different.

After some research I have found out that one needs to add the model, in my case the client model to the providers array inside config/auth.php.

So first one needs to change the api guard like this:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'clients',
    ],
],

This way to api routes login and register only take action with my clients.

Now you need to a a new provider in this case a clients provider like this.

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'clients' => [
        'driver' => 'eloquent',
        'model' => App\Client::class
    ],
],

And voila I get an access token + refresh token if I call the route.

like image 42
utdev Avatar answered Sep 19 '22 23:09

utdev