RegisterController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Jobs\SubscribeToNewsletter;
  5. use App\Recipient;
  6. use App\Rules\NotBlacklisted;
  7. use App\Rules\NotDeletedUsername;
  8. use App\User;
  9. use Illuminate\Foundation\Auth\RegistersUsers;
  10. use Illuminate\Support\Facades\Hash;
  11. use Illuminate\Support\Facades\Validator;
  12. use Ramsey\Uuid\Uuid;
  13. class RegisterController extends Controller
  14. {
  15. /*
  16. |--------------------------------------------------------------------------
  17. | Register Controller
  18. |--------------------------------------------------------------------------
  19. |
  20. | This controller handles the registration of new users as well as their
  21. | validation and creation. By default this controller uses a trait to
  22. | provide this functionality without requiring any additional code.
  23. |
  24. */
  25. use RegistersUsers;
  26. /**
  27. * Where to redirect users after registration.
  28. *
  29. * @var string
  30. */
  31. protected $redirectTo = '/';
  32. /**
  33. * Create a new controller instance.
  34. *
  35. * @return void
  36. */
  37. public function __construct()
  38. {
  39. $this->middleware('guest');
  40. }
  41. /**
  42. * Get a validator for an incoming registration request.
  43. *
  44. * @param array $data
  45. * @return \Illuminate\Contracts\Validation\Validator
  46. */
  47. protected function validator(array $data)
  48. {
  49. return Validator::make($data, [
  50. 'username' => [
  51. 'required',
  52. 'alpha_num',
  53. 'max:20',
  54. 'unique:users',
  55. new NotBlacklisted,
  56. new NotDeletedUsername
  57. ],
  58. 'email' => ['required', 'email', 'max:254', 'confirmed'],
  59. 'password' => ['required', 'min:8'],
  60. 'newsletter' => ['nullable'],
  61. 'terms' => ['required', 'accepted']
  62. ]);
  63. }
  64. /**
  65. * Create a new user instance after a valid registration.
  66. *
  67. * @param array $data
  68. * @return \App\User
  69. */
  70. protected function create(array $data)
  71. {
  72. $userId = Uuid::uuid4();
  73. $recipient = Recipient::create([
  74. 'email' => $data['email'],
  75. 'user_id' => $userId
  76. ]);
  77. if (isset($data['newsletter'])) {
  78. SubscribeToNewsletter::dispatch($data['email']);
  79. }
  80. $twoFactor = app('pragmarx.google2fa');
  81. return User::create([
  82. 'id' => $userId,
  83. 'username' => $data['username'],
  84. 'default_recipient_id' => $recipient->id,
  85. 'password' => Hash::make($data['password']),
  86. 'two_factor_secret' => $twoFactor->generateSecretKey()
  87. ]);
  88. }
  89. }