RegisterController.php 2.9 KB

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