3 namespace BookStack\Access\Controllers;
5 use BookStack\Access\LoginService;
6 use BookStack\Access\RegistrationService;
7 use BookStack\Access\SocialDriverManager;
8 use BookStack\Exceptions\StoppedAuthenticationException;
9 use BookStack\Exceptions\UserRegistrationException;
10 use BookStack\Http\Controller;
11 use Illuminate\Contracts\Validation\Validator as ValidatorContract;
12 use Illuminate\Http\Request;
13 use Illuminate\Support\Facades\Validator;
14 use Illuminate\Validation\Rules\Password;
16 class RegisterController extends Controller
18 public function __construct(
19 protected SocialDriverManager $socialDriverManager,
20 protected RegistrationService $registrationService,
21 protected LoginService $loginService
23 $this->middleware('guest');
24 $this->middleware('guard:standard');
28 * Show the application registration form.
30 * @throws UserRegistrationException
32 public function getRegister()
34 $this->registrationService->ensureRegistrationAllowed();
35 $socialDrivers = $this->socialDriverManager->getActive();
37 return view('auth.register', [
38 'socialDrivers' => $socialDrivers,
43 * Handle a registration request for the application.
45 * @throws UserRegistrationException
46 * @throws StoppedAuthenticationException
48 public function postRegister(Request $request)
50 $this->registrationService->ensureRegistrationAllowed();
51 $this->validator($request->all())->validate();
52 $userData = $request->all();
55 $user = $this->registrationService->registerUser($userData);
56 $this->loginService->login($user, auth()->getDefaultDriver());
57 } catch (UserRegistrationException $exception) {
58 if ($exception->getMessage()) {
59 $this->showErrorNotification($exception->getMessage());
62 return redirect($exception->redirectLocation);
65 $this->showSuccessNotification(trans('auth.register_success'));
71 * Get a validator for an incoming registration request.
73 protected function validator(array $data): ValidatorContract
75 return Validator::make($data, [
76 'name' => ['required', 'min:2', 'max:100'],
77 'email' => ['required', 'email', 'max:255', 'unique:users'],
78 'password' => ['required', Password::default()],
79 // Basic honey for bots that must not be filled in
80 'username' => ['prohibited'],