3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\LoginService;
6 use BookStack\Auth\Access\RegistrationService;
7 use BookStack\Auth\Access\SocialAuthService;
8 use BookStack\Auth\User;
9 use BookStack\Exceptions\StoppedAuthenticationException;
10 use BookStack\Exceptions\UserRegistrationException;
11 use BookStack\Http\Controllers\Controller;
12 use Illuminate\Foundation\Auth\RegistersUsers;
13 use Illuminate\Http\Request;
14 use Illuminate\Support\Facades\Hash;
15 use Illuminate\Support\Facades\Validator;
16 use Illuminate\Validation\Rules\Password;
18 class RegisterController extends Controller
21 |--------------------------------------------------------------------------
23 |--------------------------------------------------------------------------
25 | This controller handles the registration of new users as well as their
26 | validation and creation. By default this controller uses a trait to
27 | provide this functionality without requiring any additional code.
32 protected SocialAuthService $socialAuthService;
33 protected RegistrationService $registrationService;
34 protected LoginService $loginService;
37 * Where to redirect users after login / registration.
39 protected string $redirectTo = '/';
40 protected string $redirectPath = '/';
43 * Create a new controller instance.
45 public function __construct(
46 SocialAuthService $socialAuthService,
47 RegistrationService $registrationService,
48 LoginService $loginService
50 $this->middleware('guest');
51 $this->middleware('guard:standard');
53 $this->socialAuthService = $socialAuthService;
54 $this->registrationService = $registrationService;
55 $this->loginService = $loginService;
57 $this->redirectTo = url('/');
58 $this->redirectPath = url('/');
62 * Get a validator for an incoming registration request.
64 * @return \Illuminate\Contracts\Validation\Validator
66 protected function validator(array $data)
68 return Validator::make($data, [
69 'name' => ['required', 'min:2', 'max:100'],
70 'email' => ['required', 'email', 'max:255', 'unique:users'],
71 'password' => ['required', Password::default()],
76 * Show the application registration form.
78 * @throws UserRegistrationException
80 public function getRegister()
82 $this->registrationService->ensureRegistrationAllowed();
83 $socialDrivers = $this->socialAuthService->getActiveDrivers();
85 return view('auth.register', [
86 'socialDrivers' => $socialDrivers,
91 * Handle a registration request for the application.
93 * @throws UserRegistrationException
94 * @throws StoppedAuthenticationException
96 public function postRegister(Request $request)
98 $this->registrationService->ensureRegistrationAllowed();
99 $this->validator($request->all())->validate();
100 $userData = $request->all();
103 $user = $this->registrationService->registerUser($userData);
104 $this->loginService->login($user, auth()->getDefaultDriver());
105 } catch (UserRegistrationException $exception) {
106 if ($exception->getMessage()) {
107 $this->showErrorNotification($exception->getMessage());
110 return redirect($exception->redirectLocation);
113 $this->showSuccessNotification(trans('auth.register_success'));
115 return redirect($this->redirectPath());
119 * Create a new user instance after a valid registration.
125 protected function create(array $data)
127 return User::create([
128 'name' => $data['name'],
129 'email' => $data['email'],
130 'password' => Hash::make($data['password']),