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\UserRegistrationException;
10 use BookStack\Http\Controllers\Controller;
11 use Illuminate\Foundation\Auth\RegistersUsers;
12 use Illuminate\Http\Request;
13 use Illuminate\Support\Facades\Hash;
16 class RegisterController extends Controller
19 |--------------------------------------------------------------------------
21 |--------------------------------------------------------------------------
23 | This controller handles the registration of new users as well as their
24 | validation and creation. By default this controller uses a trait to
25 | provide this functionality without requiring any additional code.
31 protected $socialAuthService;
32 protected $registrationService;
33 protected $loginService;
36 * Where to redirect users after login / registration.
40 protected $redirectTo = '/';
41 protected $redirectPath = '/';
44 * Create a new controller instance.
46 public function __construct(
47 SocialAuthService $socialAuthService,
48 RegistrationService $registrationService,
49 LoginService $loginService
52 $this->middleware('guest');
53 $this->middleware('guard:standard');
55 $this->socialAuthService = $socialAuthService;
56 $this->registrationService = $registrationService;
57 $this->loginService = $loginService;
59 $this->redirectTo = url('/');
60 $this->redirectPath = url('/');
64 * Get a validator for an incoming registration request.
66 * @return \Illuminate\Contracts\Validation\Validator
68 protected function validator(array $data)
70 return Validator::make($data, [
71 'name' => 'required|min:2|max:255',
72 'email' => 'required|email|max:255|unique:users',
73 'password' => 'required|min:8',
78 * Show the application registration form.
80 * @throws UserRegistrationException
82 public function getRegister()
84 $this->registrationService->ensureRegistrationAllowed();
85 $socialDrivers = $this->socialAuthService->getActiveDrivers();
87 return view('auth.register', [
88 'socialDrivers' => $socialDrivers,
93 * Handle a registration request for the application.
95 * @throws UserRegistrationException
97 public function postRegister(Request $request)
99 $this->registrationService->ensureRegistrationAllowed();
100 $this->validator($request->all())->validate();
101 $userData = $request->all();
104 $user = $this->registrationService->registerUser($userData);
105 $this->loginService->login($user, auth()->getDefaultDriver());
106 } catch (UserRegistrationException $exception) {
107 if ($exception->getMessage()) {
108 $this->showErrorNotification($exception->getMessage());
111 return redirect($exception->redirectLocation);
114 $this->showSuccessNotification(trans('auth.register_success'));
116 return redirect($this->redirectPath());
120 * Create a new user instance after a valid registration.
126 protected function create(array $data)
128 return User::create([
129 'name' => $data['name'],
130 'email' => $data['email'],
131 'password' => Hash::make($data['password']),