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

Commit ff09c40

Browse files
authored
Merge pull request #84 from joselfonseca/feature/78-forgot-password
Forgot Password flow
2 parents 0e477b4 + 01e6543 commit ff09c40

File tree

12 files changed

+404
-98
lines changed

12 files changed

+404
-98
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Foundation\Events\Dispatchable;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
class ForgotPasswordRequested
10+
{
11+
use Dispatchable, InteractsWithSockets, SerializesModels;
12+
13+
public $email;
14+
15+
public function __construct(string $email)
16+
{
17+
$this->email = $email;
18+
}
19+
}

app/Events/PasswordRecovered.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\User;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class PasswordRecovered
11+
{
12+
use Dispatchable, InteractsWithSockets, SerializesModels;
13+
14+
public $user;
15+
16+
public function __construct(User $user)
17+
{
18+
$this->user = $user;
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use Exception;
6+
7+
class EmailNotSentException extends Exception
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use Exception;
6+
7+
class PasswordNotUpdated extends Exception
8+
{
9+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Auth;
4+
5+
use App\Events\ForgotPasswordRequested;
6+
use App\Events\PasswordRecovered;
7+
use App\Exceptions\EmailNotSentException;
8+
use App\Exceptions\PasswordNotUpdated;
9+
use App\Http\Controllers\Controller;
10+
use App\Models\User;
11+
use Illuminate\Http\Request;
12+
use Illuminate\Support\Facades\Hash;
13+
use Illuminate\Support\Facades\Password;
14+
15+
class PasswordsController extends Controller
16+
{
17+
public function store(Request $request)
18+
{
19+
$this->validate($request, [
20+
'email' => 'required|email|exists:users,email',
21+
]);
22+
23+
$response = $this->broker()->sendResetLink(['email' => $request->get('email')]);
24+
25+
if ($response == Password::RESET_LINK_SENT) {
26+
event(new ForgotPasswordRequested($request->get('email')));
27+
28+
return response()->json(null, 201);
29+
}
30+
31+
throw new EmailNotSentException(__($response));
32+
}
33+
34+
public function update(Request $request)
35+
{
36+
$this->validate($request, [
37+
'email' => 'required|email|exists:users,email',
38+
'token' => 'required|exists:password_resets,token',
39+
'password' => 'required|min:8',
40+
]);
41+
42+
$response = $this->broker()->reset($request->except('_token'), function ($user, $password) {
43+
$user->password = Hash::make($password);
44+
$user->save();
45+
});
46+
47+
if ($response == Password::PASSWORD_RESET) {
48+
event(new PasswordRecovered(User::where('email', $request->get('email'))->first()));
49+
50+
return response()->json([
51+
'message' => __($response),
52+
]);
53+
}
54+
55+
throw new PasswordNotUpdated(__($response));
56+
}
57+
58+
public function broker()
59+
{
60+
return Password::broker();
61+
}
62+
}

0 commit comments

Comments
 (0)