RegisterController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Classes\Pterodactyl;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Configuration;
  6. use App\Models\User;
  7. use App\Providers\RouteServiceProvider;
  8. use Illuminate\Foundation\Auth\RegistersUsers;
  9. use Illuminate\Support\Facades\Hash;
  10. use Illuminate\Support\Facades\Validator;
  11. use Illuminate\Validation\ValidationException;
  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 = RouteServiceProvider::HOME;
  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. //check if ip has already made an account
  49. $data['ip'] = session()->get('ip') ?? request()->ip();
  50. if (User::where('ip', '=', request()->ip())->exists()) session()->put('ip', request()->ip());
  51. return Validator::make($data, [
  52. 'name' => ['required', 'string', 'max:30', 'min:4', 'alpha_num', 'unique:users'],
  53. 'email' => ['required', 'string', 'email', 'max:64', 'unique:users'],
  54. 'password' => ['required', 'string', 'min:8', 'confirmed'],
  55. 'g-recaptcha-response' => ['recaptcha'],
  56. 'ip' => ['unique:users'],
  57. ], [
  58. 'ip.unique' => "You have already made an account with us! Please contact support if you think this is incorrect."
  59. ]);
  60. }
  61. /**
  62. * Create a new user instance after a valid registration.
  63. *
  64. * @param array $data
  65. * @return User|\Illuminate\Http\RedirectResponse
  66. */
  67. protected function create(array $data)
  68. {
  69. $user = User::create([
  70. 'name' => $data['name'],
  71. 'email' => $data['email'],
  72. 'credits' => Configuration::getValueByKey('INITIAL_CREDITS'),
  73. 'server_limit' => Configuration::getValueByKey('INITIAL_SERVER_LIMIT'),
  74. 'password' => Hash::make($data['password']),
  75. ]);
  76. $response = Pterodactyl::client()->post('/application/users', [
  77. "external_id" => (string)$user->id,
  78. "username" => $user->name,
  79. "email" => $user->email,
  80. "first_name" => $user->name,
  81. "last_name" => $user->name,
  82. "password" => $data['password'],
  83. "root_admin" => false,
  84. "language" => "en"
  85. ]);
  86. if ($response->failed()) {
  87. $user->delete();
  88. redirect()->route('register')->with('error', 'pterodactyl error');
  89. return $user;
  90. }
  91. $user->update([
  92. 'pterodactyl_id' => $response->json()['attributes']['id']
  93. ]);
  94. return $user;
  95. }
  96. }