Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit 7ccbde2

Browse files
committed
Save E-Mail Address correctly on registration
1 parent e5081ea commit 7ccbde2

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

app/Http/Controllers/Auth/RegisterController.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
namespace App\Http\Controllers\Auth;
44

55
use App\Http\Controllers\Controller;
6+
use App\Http\Controllers\SettingsController;
67
use App\Providers\RouteServiceProvider;
78
use App\User;
9+
use App\UserEmail;
810
use Carbon\Carbon;
11+
use Exception;
912
use Illuminate\Foundation\Auth\RegistersUsers;
1013
use Illuminate\Support\Facades\Hash;
1114
use Illuminate\Support\Facades\Validator;
1215

13-
class RegisterController extends Controller
14-
{
16+
class RegisterController extends Controller {
1517
/*
1618
|--------------------------------------------------------------------------
1719
| Register Controller
@@ -30,15 +32,14 @@ class RegisterController extends Controller
3032
*
3133
* @var string
3234
*/
33-
protected $redirectTo = RouteServiceProvider::HOME;
35+
protected string $redirectTo = RouteServiceProvider::HOME;
3436

3537
/**
3638
* Create a new controller instance.
3739
*
3840
* @return void
3941
*/
40-
public function __construct()
41-
{
42+
public function __construct() {
4243
$this->middleware('guest');
4344
}
4445

@@ -48,8 +49,7 @@ public function __construct()
4849
* @param array $data
4950
* @return \Illuminate\Contracts\Validation\Validator
5051
*/
51-
protected function validator(array $data)
52-
{
52+
protected function validator(array $data): \Illuminate\Contracts\Validation\Validator {
5353
return Validator::make($data, [
5454
'username' => ['required', 'string', 'max:255'],
5555
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
@@ -61,15 +61,28 @@ protected function validator(array $data)
6161
* Create a new user instance after a valid registration.
6262
*
6363
* @param array $data
64-
* @return \App\User
64+
* @return User
6565
*/
66-
protected function create(array $data)
67-
{
68-
return User::create([
69-
'username' => $data['username'],
70-
'email' => $data['email'],
71-
'password' => Hash::make($data['password']),
72-
'last_login' => Carbon::now()
73-
]);
66+
protected function create(array $data): User {
67+
$user = User::create([
68+
'username' => $data['username'],
69+
'email' => $data['email'],
70+
'password' => Hash::make($data['password']),
71+
'last_login' => Carbon::now()
72+
]);
73+
74+
75+
$userEmail = UserEmail::create([
76+
'email' => $user->email,
77+
'unverified_user_id' => $user->id,
78+
'verification_key' => md5(rand(0, 99999) . time() . $user->id)
79+
]);
80+
81+
try {
82+
SettingsController::sendEmailVerification($userEmail);
83+
} catch(Exception $e) {
84+
report($e);
85+
}
86+
return $user;
7487
}
7588
}

app/Http/Controllers/SettingsController.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\UserEmail;
99
use App\UserSettings;
1010
use Carbon\Carbon;
11+
use Exception;
1112
use Illuminate\Contracts\Support\Renderable;
1213
use Illuminate\Http\RedirectResponse;
1314
use Illuminate\Http\Request;
@@ -62,19 +63,30 @@ public function addEmail(Request $request): RedirectResponse {
6263
]);
6364

6465
try {
65-
Mail::to($userEmail->email)->send(new MailVerificationMessage($userEmail));
66-
67-
if(Mail::failures())
68-
throw new \Exception("Failure on sending mail: " . json_encode(Mail::failures()));
66+
self::sendEmailVerification($userEmail);
6967

7068
return back()->with('alert-success', __('settings.verify_mail.alert_save'));
71-
} catch(\Exception $e) {
69+
} catch(Exception $e) {
7270
report($e);
7371
$userEmail->delete();
7472
return back()->with('alert-danger', __('settings.verify_mail.alert_save_error'));
7573
}
7674
}
7775

76+
/**
77+
* @param UserEmail $userEmail
78+
* @return bool
79+
* @throws Exception
80+
*/
81+
public static function sendEmailVerification(UserEmail $userEmail): bool {
82+
Mail::to($userEmail->email)->send(new MailVerificationMessage($userEmail));
83+
84+
if(Mail::failures())
85+
throw new Exception("Failure on sending mail: " . json_encode(Mail::failures()));
86+
87+
return true;
88+
}
89+
7890
public function deleteEmail(Request $request): RedirectResponse {
7991
$validated = $request->validate([
8092
'id' => ['required', 'exists:user_emails,id']

0 commit comments

Comments
 (0)