3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\RegistrationService;
6 use BookStack\Auth\Access\SocialAuthService;
7 use BookStack\Auth\User;
8 use BookStack\Exceptions\UserRegistrationException;
9 use BookStack\Http\Controllers\Controller;
10 use Illuminate\Foundation\Auth\RegistersUsers;
11 use Illuminate\Http\Request;
12 use Illuminate\Support\Facades\Hash;
15 class RegisterController extends Controller
18 |--------------------------------------------------------------------------
20 |--------------------------------------------------------------------------
22 | This controller handles the registration of new users as well as their
23 | validation and creation. By default this controller uses a trait to
24 | provide this functionality without requiring any additional code.
30 protected $socialAuthService;
31 protected $registrationService;
34 * Where to redirect users after login / registration.
38 protected $redirectTo = '/';
39 protected $redirectPath = '/';
42 * Create a new controller instance.
44 public function __construct(SocialAuthService $socialAuthService, RegistrationService $registrationService)
46 $this->middleware('guest')->only(['getRegister', 'postRegister']);
48 $this->socialAuthService = $socialAuthService;
49 $this->registrationService = $registrationService;
51 $this->redirectTo = url('/');
52 $this->redirectPath = url('/');
53 parent::__construct();
57 * Get a validator for an incoming registration request.
59 * @return \Illuminate\Contracts\Validation\Validator
61 protected function validator(array $data)
63 return Validator::make($data, [
64 'name' => 'required|min:2|max:255',
65 'email' => 'required|email|max:255|unique:users',
66 'password' => 'required|min:8',
71 * Show the application registration form.
72 * @throws UserRegistrationException
74 public function getRegister()
76 $this->registrationService->checkRegistrationAllowed();
77 $socialDrivers = $this->socialAuthService->getActiveDrivers();
78 return view('auth.register', [
79 'socialDrivers' => $socialDrivers,
84 * Handle a registration request for the application.
85 * @throws UserRegistrationException
87 public function postRegister(Request $request)
89 $this->registrationService->checkRegistrationAllowed();
90 $this->validator($request->all())->validate();
91 $userData = $request->all();
94 $this->registrationService->registerUser($userData);
95 } catch (UserRegistrationException $exception) {
96 if ($exception->getMessage()) {
97 $this->showErrorNotification($exception->getMessage());
99 return redirect($exception->redirectLocation);
102 $this->showSuccessNotification(trans('auth.register_success'));
103 return redirect($this->redirectPath());
107 * Create a new user instance after a valid registration.
111 protected function create(array $data)
113 return User::create([
114 'name' => $data['name'],
115 'email' => $data['email'],
116 'password' => Hash::make($data['password']),