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
53 $this->middleware('guest');
54 $this->middleware('guard:standard');
56 $this->socialAuthService = $socialAuthService;
57 $this->registrationService = $registrationService;
58 $this->loginService = $loginService;
60 $this->redirectTo = url('/');
61 $this->redirectPath = url('/');
65 * Get a validator for an incoming registration request.
67 * @return \Illuminate\Contracts\Validation\Validator
69 protected function validator(array $data)
71 return Validator::make($data, [
72 'name' => 'required|min:2|max:255',
73 'email' => 'required|email|max:255|unique:users',
74 'password' => 'required|min:8',
79 * Show the application registration form.
81 * @throws UserRegistrationException
83 public function getRegister()
85 $this->registrationService->ensureRegistrationAllowed();
86 $socialDrivers = $this->socialAuthService->getActiveDrivers();
88 return view('auth.register', [
89 'socialDrivers' => $socialDrivers,
94 * Handle a registration request for the application.
96 * @throws UserRegistrationException
97 * @throws StoppedAuthenticationException
99 public function postRegister(Request $request)
101 $this->registrationService->ensureRegistrationAllowed();
102 $this->validator($request->all())->validate();
103 $userData = $request->all();
106 $user = $this->registrationService->registerUser($userData);
107 $this->loginService->login($user, auth()->getDefaultDriver());
108 } catch (UserRegistrationException $exception) {
109 if ($exception->getMessage()) {
110 $this->showErrorNotification($exception->getMessage());
113 return redirect($exception->redirectLocation);
116 $this->showSuccessNotification(trans('auth.register_success'));
118 return redirect($this->redirectPath());
122 * Create a new user instance after a valid registration.
128 protected function create(array $data)
130 return User::create([
131 'name' => $data['name'],
132 'email' => $data['email'],
133 'password' => Hash::make($data['password']),