I am developing an API in Laravel relying on the authentication provided by Passport , populating the development database with Seeders and testing with the Postman client .
The problem is the following:
When populating my Users table with the Seeders and trying to login against the route
POST: /oauth/tokens
that Passport generates with one of the populated users, it doesn't work, the message it responds to 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 to login then it works.
I need to be able to use the populated users for login so that I am not creating the users by hand so that they can use the oauth.
The configuration of my passport client is of the password type, the other configuration/installation of passport is well done and what works when I create a user by hand. The User entity 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
I deduce that the problem is that
It doesn't decrypt or validate the
bcrypt
.Why does he
controller
work butseeder
he doesn't?In
controller
it you are registering it without encryption and inseeder
it you do it withbcrypt()
. The solution would be to leave theOr else it gives you a solution, because the next step is to find out what password verification the Passport has