Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Forgot Password flow #84

Merged
merged 2 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/Events/ForgotPasswordRequested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ForgotPasswordRequested
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public $email;

public function __construct(string $email)
{
$this->email = $email;
}
}
20 changes: 20 additions & 0 deletions app/Events/PasswordRecovered.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Events;

use App\Models\User;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PasswordRecovered
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public $user;

public function __construct(User $user)
{
$this->user = $user;
}
}
9 changes: 9 additions & 0 deletions app/Exceptions/EmailNotSentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Exceptions;

use Exception;

class EmailNotSentException extends Exception
{
}
9 changes: 9 additions & 0 deletions app/Exceptions/PasswordNotUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Exceptions;

use Exception;

class PasswordNotUpdated extends Exception
{
}
62 changes: 62 additions & 0 deletions app/Http/Controllers/Api/Auth/PasswordsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Http\Controllers\Api\Auth;

use App\Events\ForgotPasswordRequested;
use App\Events\PasswordRecovered;
use App\Exceptions\EmailNotSentException;
use App\Exceptions\PasswordNotUpdated;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;

class PasswordsController extends Controller
{
public function store(Request $request)
{
$this->validate($request, [
'email' => 'required|email|exists:users,email',
]);

$response = $this->broker()->sendResetLink(['email' => $request->get('email')]);

if ($response == Password::RESET_LINK_SENT) {
event(new ForgotPasswordRequested($request->get('email')));

return response()->json(null, 201);
}

throw new EmailNotSentException(__($response));
}

public function update(Request $request)
{
$this->validate($request, [
'email' => 'required|email|exists:users,email',
'token' => 'required|exists:password_resets,token',
'password' => 'required|min:8',
]);

$response = $this->broker()->reset($request->except('_token'), function ($user, $password) {
$user->password = Hash::make($password);
$user->save();
});

if ($response == Password::PASSWORD_RESET) {
event(new PasswordRecovered(User::where('email', $request->get('email'))->first()));

return response()->json([
'message' => __($response),
]);
}

throw new PasswordNotUpdated(__($response));
}

public function broker()
{
return Password::broker();
}
}
Loading