3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Actions\ActivityType;
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\Facades\Theme;
11 use BookStack\Http\Controllers\Controller;
12 use BookStack\Theming\ThemeEvents;
13 use Illuminate\Foundation\Auth\RegistersUsers;
14 use Illuminate\Http\Request;
15 use Illuminate\Support\Facades\Hash;
18 class RegisterController extends Controller
21 |--------------------------------------------------------------------------
23 |--------------------------------------------------------------------------
25 | This controller handles the registration of new users as well as their
26 | validation and creation. By default this controller uses a trait to
27 | provide this functionality without requiring any additional code.
33 protected $socialAuthService;
34 protected $registrationService;
37 * Where to redirect users after login / registration.
41 protected $redirectTo = '/';
42 protected $redirectPath = '/';
45 * Create a new controller instance.
47 public function __construct(SocialAuthService $socialAuthService, RegistrationService $registrationService)
49 $this->middleware('guest');
50 $this->middleware('guard:standard');
52 $this->socialAuthService = $socialAuthService;
53 $this->registrationService = $registrationService;
55 $this->redirectTo = url('/');
56 $this->redirectPath = url('/');
60 * Get a validator for an incoming registration request.
62 * @return \Illuminate\Contracts\Validation\Validator
64 protected function validator(array $data)
66 return Validator::make($data, [
67 'name' => 'required|min:2|max:255',
68 'email' => 'required|email|max:255|unique:users',
69 'password' => 'required|min:8',
74 * Show the application registration form.
76 * @throws UserRegistrationException
78 public function getRegister()
80 $this->registrationService->ensureRegistrationAllowed();
81 $socialDrivers = $this->socialAuthService->getActiveDrivers();
83 return view('auth.register', [
84 'socialDrivers' => $socialDrivers,
89 * Handle a registration request for the application.
91 * @throws UserRegistrationException
93 public function postRegister(Request $request)
95 $this->registrationService->ensureRegistrationAllowed();
96 $this->validator($request->all())->validate();
97 $userData = $request->all();
100 $user = $this->registrationService->registerUser($userData);
101 auth()->login($user);
102 Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
103 $this->logActivity(ActivityType::AUTH_LOGIN, $user);
104 } catch (UserRegistrationException $exception) {
105 if ($exception->getMessage()) {
106 $this->showErrorNotification($exception->getMessage());
109 return redirect($exception->redirectLocation);
112 $this->showSuccessNotification(trans('auth.register_success'));
114 return redirect($this->redirectPath());
118 * Create a new user instance after a valid registration.
124 protected function create(array $data)
126 return User::create([
127 'name' => $data['name'],
128 'email' => $data['email'],
129 'password' => Hash::make($data['password']),