3 namespace BookStack\Access\Controllers;
5 use BookStack\Access\LoginService;
6 use BookStack\Access\RegistrationService;
7 use BookStack\Access\SocialAuthService;
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 protected SocialAuthService $socialAuthService;
19 protected RegistrationService $registrationService;
20 protected LoginService $loginService;
23 * Create a new controller instance.
25 public function __construct(
26 SocialAuthService $socialAuthService,
27 RegistrationService $registrationService,
28 LoginService $loginService
30 $this->middleware('guest');
31 $this->middleware('guard:standard');
33 $this->socialAuthService = $socialAuthService;
34 $this->registrationService = $registrationService;
35 $this->loginService = $loginService;
39 * Show the application registration form.
41 * @throws UserRegistrationException
43 public function getRegister()
45 $this->registrationService->ensureRegistrationAllowed();
46 $socialDrivers = $this->socialAuthService->getActiveDrivers();
48 return view('auth.register', [
49 'socialDrivers' => $socialDrivers,
54 * Handle a registration request for the application.
56 * @throws UserRegistrationException
57 * @throws StoppedAuthenticationException
59 public function postRegister(Request $request)
61 $this->registrationService->ensureRegistrationAllowed();
62 $this->validator($request->all())->validate();
63 $userData = $request->all();
66 $user = $this->registrationService->registerUser($userData);
67 $this->loginService->login($user, auth()->getDefaultDriver());
68 } catch (UserRegistrationException $exception) {
69 if ($exception->getMessage()) {
70 $this->showErrorNotification($exception->getMessage());
73 return redirect($exception->redirectLocation);
76 $this->showSuccessNotification(trans('auth.register_success'));
82 * Get a validator for an incoming registration request.
84 protected function validator(array $data): ValidatorContract
86 return Validator::make($data, [
87 'name' => ['required', 'min:2', 'max:100'],
88 'email' => ['required', 'email', 'max:255', 'unique:users'],
89 'password' => ['required', Password::default()],