RegisterController.php 2.4 KB

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