|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Devpri\Tinre\Http\Controllers\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Devpri\Tinre\Events\EmailChangeCreated; |
|
7
|
|
|
use Devpri\Tinre\Http\Controllers\Controller; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Support\Facades\DB; |
|
11
|
|
|
use Illuminate\Support\Facades\Hash; |
|
12
|
|
|
use Illuminate\Support\Str; |
|
13
|
|
|
|
|
14
|
|
|
class ChangeEmailController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
/* |
|
17
|
|
|
|-------------------------------------------------------------------------- |
|
18
|
|
|
| Change Email Controller |
|
19
|
|
|
|-------------------------------------------------------------------------- |
|
20
|
|
|
| |
|
21
|
|
|
| This controller handles user email changes. |
|
22
|
|
|
| |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
1 |
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->middleware('auth')->only('create'); |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Where to redirect users after verification. |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function redirectTo() |
|
34
|
|
|
{ |
|
35
|
1 |
|
return route('dashboard'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function create(Request $request) |
|
39
|
|
|
{ |
|
40
|
1 |
|
$this->validateData($request); |
|
41
|
|
|
|
|
42
|
1 |
|
$user = $request->user(); |
|
43
|
|
|
|
|
44
|
1 |
|
if (! Hash::check($request->password, $user->password)) { |
|
45
|
|
|
return response()->json(['message' => __('Invalid Password')], 403); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$maxGenerationAttempts = 5; |
|
49
|
|
|
|
|
50
|
1 |
|
while ($maxGenerationAttempts-- > 0) { |
|
51
|
|
|
try { |
|
52
|
|
|
$data = [ |
|
53
|
1 |
|
'email' => $request->email, |
|
54
|
1 |
|
'token' => Str::random(60), |
|
55
|
1 |
|
'created_at' => Carbon::now(), |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
1 |
|
DB::table('email_changes')->updateOrInsert(['user_id' => $user->id], $data); |
|
59
|
|
|
} catch (Exception $e) { |
|
60
|
|
|
if ($maxGenerationAttempts === 0) { |
|
61
|
|
|
throw $e; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
event(new EmailChangeCreated($data)); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
1 |
|
return response()->json(['message' => __('Please verify your email.')]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
public function change(Request $request, $token) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
1 |
|
$email = DB::table('email_changes')->where('token', $token)->first(); |
|
74
|
|
|
|
|
75
|
1 |
|
if (! $email) { |
|
76
|
|
|
abort(404); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
DB::table('users')->where('id', $email->user_id)->update([ |
|
80
|
1 |
|
'email' => $email->email, |
|
81
|
1 |
|
'email_verified_at' => Carbon::now(), |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
1 |
|
DB::table('email_changes')->where('token', $token)->delete(); |
|
85
|
|
|
|
|
86
|
1 |
|
return redirect($this->redirectTo())->with('status', __('Email changed.')); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
protected function validateData(Request $request) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$request->validate([ |
|
92
|
1 |
|
'email' => ['required', 'email', 'unique:users'], |
|
93
|
|
|
'password' => ['required'], |
|
94
|
|
|
]); |
|
95
|
1 |
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|