How to reconfigure APP_KEY from an Laravel 5.1 application

Asked

Viewed 426 times

0

Today I restored my Macbook Pro because I had some incompatibilities with the El Capitan (beta).

During this process I forgot to backup the file .env of my project, so I could no longer login to the application (on account of APP_KEY).

Obviously I understand why, but what I couldn’t figure out was how to fix it.

Initially I tried the following:

php artisan key:generate

With that I’ve managed a new APP_KEY right? So now I need to update all passwords:

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

Okay, I’ve updated and I can log in, great, right? No! When I try to update some password by the system (/admin), I can no longer login after that.

Below follows the code of the method update($id):

$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 I’m 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, please let me know.

1 answer

0

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

At the beginning of the project I had a mutator for the attribute password.

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

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

Anyway, the solution to the problem was not to encrypt the password in the controller:

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

Thank you for your time!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.