Skip to content

Commit

Permalink
add a password reset command #102
Browse files Browse the repository at this point in the history
  • Loading branch information
fr0tt committed Oct 7, 2024
1 parent 0bee04c commit 547547c
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions app/Console/Commands/ResetPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class ResetPassword extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'reset:password';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Reset a password for an user';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{

$this->info('Type in the email of the user you would like to reset');

$email = $this->ask('Email');

$validator = Validator::make([
'email' => $email
], [
'email' => 'email',
]);

if ($validator->fails()) {
$this->error($validator->errors()->first());
return Command::FAILURE;
}

$user = User::where('email', $email)->first();

if ($user === null) {
$this->error('User does not exist.');
return Command::FAILURE;
}

$this->info('Type in a new password');
$password = $this->secret('Password');
$password2 = $this->secret('Re-entered password');

if ($password !== $password2) {
$this->error('Re-entered password does not match password');
return Command::FAILURE;
}

$user->password = Hash::make($password);
$user->save();

return Command::SUCCESS;
}

}

0 comments on commit 547547c

Please sign in to comment.