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;
17 class RegisterController extends Controller
20 |--------------------------------------------------------------------------
22 |--------------------------------------------------------------------------
24 | This controller handles the registration of new users as well as their
25 | validation and creation. By default this controller uses a trait to
26 | provide this functionality without requiring any additional code.
32 protected $socialAuthService;
33 protected $registrationService;
34 protected $loginService;
37 * Where to redirect users after login / registration.
41 protected $redirectTo = '/';
42 protected $redirectPath = '/';
45 * Create a new controller instance.
47 public function __construct(
48 SocialAuthService $socialAuthService,
49 RegistrationService $registrationService,
50 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
96 * @throws StoppedAuthenticationException
98 public function postRegister(Request $request)
100 $this->registrationService->ensureRegistrationAllowed();
101 $this->validator($request->all())->validate();
102 $userData = $request->all();
105 $user = $this->registrationService->registerUser($userData);
106 $this->loginService->login($user, auth()->getDefaultDriver());
107 } catch (UserRegistrationException $exception) {
108 if ($exception->getMessage()) {
109 $this->showErrorNotification($exception->getMessage());
112 return redirect($exception->redirectLocation);
115 $this->showSuccessNotification(trans('auth.register_success'));
117 return redirect($this->redirectPath());
121 * Create a new user instance after a valid registration.
127 protected function create(array $data)
129 return User::create([
130 'name' => $data['name'],
131 'email' => $data['email'],
132 'password' => Hash::make($data['password']),