Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to regenerate APP_KEY and set new passwords in Laravel 5

Today I restore my Macbook OS Yosemite and I forgot to backup my .env file, so I lost my APP_KEY.

Now all my paswords not work anymore and I understand that. So I do that:

php artisan key:generate

Then I update all passwords using this code:

Route::get('/', function () {
    $users = new Project\DB\User;
    $users->update(['password' => Hash::make('newPasswordHere')]);
});

But when I try to update an user using the system and I change the password, I can't login. That's the code of update($id) method:

$user = User::getById($id);

$user->role_id = $request->get('role_id');
$user->first_name = $request->get('first_name');
$user->last_name = $request->get('last_name');
$user->email = $request->get('email');

if ($request->has('password')) {
    $user->password = Hash::make($request->get('password'));
}

$user->active = $request->get('active');
$user->update();

What am I doing wrong?


My setup

  • Macbook Pro - Yosemite
  • PHP-FPM - PHP 5.6.11 (cli) (built: Jul 19 2015 15:15:07)
  • Nginx - nginx version: nginx/1.8.0

If you need more information, tell me please.

like image 302
Patrick Maciel Avatar asked Dec 06 '25 07:12

Patrick Maciel


1 Answers

I do not believe that was the problem, but I must tell them the real cause and solution:

At the beginning of the project I was set a mutator to password attribute.

This mutator in turn was already encrypting the password (yes ... I know, I do not believe I did not see it).

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = bcrypt($value);
}

Finally, the solution of the problem was not encrypt the password on the controller:

if ($request->has('password')) {
    $user->password = $request->get('password');
}

Sorry and thank you for your time!

like image 70
Patrick Maciel Avatar answered Dec 07 '25 22:12

Patrick Maciel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!