User Seeds Laravel do not work correctly with Passport

3

I am developing an API in Laravel by relying on the authentication it provides Passport , populating the development database with the Seeders and performing the tests with the client Postman .

The problem is this:

  

By populating my Users table with Seeders and trying to login against POST: /oauth/tokens what Passport generates with one of the populated users does not work, the message that responds is always the same: invalid credentials. But, if I register a new user with my controller that does the same thing as the seeder , User::create([...]) and then try login then it works.

I need to be able to use populated users for login so that users are not creating by hand so they can use the oauth.

The configuration of my passport client is a password type, the other configuration / installation of passport is well done and what works when I create a user by hand. The entity User has the HasApiTokens trait.

Where is the problem?

Update

  

UserController

<?php

...

use App\User;

class UserController extends Controller
{

    public function store(Request $request)
    {
        try {
            $this->validate($request, User::STORE_RULES);

            DB::beginTransaction();

            $input = $request->all();
            $user = User::create($input);

            DB::commit();

            return Response::json([
                'code' => 200,
                'message' => 'OK',
                'url' => url("/api/v1/users"),
                'data' => User::whereId($user->id)->first(),
            ]);
        } catch (ValidationException $e) {
            return Response::json([
                'code' => 400,
                'message' => $e->getMessage(),
                'url' => url("/api/v1/users"),
                'data' => $e->validator->errors()->all(),
            ]);
        } catch (QueryException $e) {
            DB::rollback();
            return Response::json([
                'code' => 500,
                'message' => 'KO',
                'url' => url("/api/v1/users"),
                'data' => [],
            ]);
        }
}

...
  

UserTableSeeder

<?php

use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        factory(App\User::class, 1)->create()->each(function ($user) {
            $user->name = 'username';
            $user->phone = '666 666 666';
            $user->email = '[email protected]';
            $user->password = bcrypt('root');
            $user->role_id = App\Role::whereName('admin')->first()->id;
            $user->save();
        });
        factory(App\User::class, 19)->create();
    }
}
  

Process

composer install
php artisan key:generate
php artisan migrate
php artisan passport:client --password
php artisan passport:keys
php artisan config:cache
php artisan db:seed
    
asked by dddenis 04.04.2017 в 09:35
source

1 answer

1

I assume the problem is that

  

the authentication provided by Passport

Does not decrypt or does not validate the bcrypt .

Why does the controller serve but the seeder do not?

In controller you are registering without encryption and in seeder you do it with bcrypt() . The solution would be to leave the

  

UserTableSeeder

<?php

use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        factory(App\User::class, 1)->create()->each(function ($user) {
            $user->name = 'username';
            $user->phone = '666 666 666';
            $user->email = '[email protected]';
            $user->password = 'root';
            $user->role_id = App\Role::whereName('admin')->first()->id;
            $user->save();
        });
        factory(App\User::class, 19)->create();
    }
}

Or else it gives you a solution, because the next step is to find out which password check has the Passport

    
answered by 13.04.2017 / 06:40
source