Skip to content

Commit

Permalink
Save E-Mail Address correctly on registration
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu committed Dec 30, 2020
1 parent e5081ea commit 7ccbde2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
45 changes: 29 additions & 16 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Controllers\SettingsController;
use App\Providers\RouteServiceProvider;
use App\User;
use App\UserEmail;
use Carbon\Carbon;
use Exception;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
class RegisterController extends Controller {
/*
|--------------------------------------------------------------------------
| Register Controller
Expand All @@ -30,15 +32,14 @@ class RegisterController extends Controller
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
protected string $redirectTo = RouteServiceProvider::HOME;

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
public function __construct() {
$this->middleware('guest');
}

Expand All @@ -48,8 +49,7 @@ public function __construct()
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
protected function validator(array $data): \Illuminate\Contracts\Validation\Validator {
return Validator::make($data, [
'username' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
Expand All @@ -61,15 +61,28 @@ protected function validator(array $data)
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
* @return User
*/
protected function create(array $data)
{
return User::create([
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'last_login' => Carbon::now()
]);
protected function create(array $data): User {
$user = User::create([
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'last_login' => Carbon::now()
]);


$userEmail = UserEmail::create([
'email' => $user->email,
'unverified_user_id' => $user->id,
'verification_key' => md5(rand(0, 99999) . time() . $user->id)
]);

try {
SettingsController::sendEmailVerification($userEmail);
} catch(Exception $e) {
report($e);
}
return $user;
}
}
22 changes: 17 additions & 5 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\UserEmail;
use App\UserSettings;
use Carbon\Carbon;
use Exception;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -62,19 +63,30 @@ public function addEmail(Request $request): RedirectResponse {
]);

try {
Mail::to($userEmail->email)->send(new MailVerificationMessage($userEmail));

if(Mail::failures())
throw new \Exception("Failure on sending mail: " . json_encode(Mail::failures()));
self::sendEmailVerification($userEmail);

return back()->with('alert-success', __('settings.verify_mail.alert_save'));
} catch(\Exception $e) {
} catch(Exception $e) {
report($e);
$userEmail->delete();
return back()->with('alert-danger', __('settings.verify_mail.alert_save_error'));
}
}

/**
* @param UserEmail $userEmail
* @return bool
* @throws Exception
*/
public static function sendEmailVerification(UserEmail $userEmail): bool {
Mail::to($userEmail->email)->send(new MailVerificationMessage($userEmail));

if(Mail::failures())
throw new Exception("Failure on sending mail: " . json_encode(Mail::failures()));

return true;
}

public function deleteEmail(Request $request): RedirectResponse {
$validated = $request->validate([
'id' => ['required', 'exists:user_emails,id']
Expand Down

0 comments on commit 7ccbde2

Please sign in to comment.