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.
75 * @throws UserRegistrationException
77 public function getRegister()
79 $this->registrationService->ensureRegistrationAllowed();
80 $socialDrivers = $this->socialAuthService->getActiveDrivers();
81 return view('auth.register', [
82 'socialDrivers' => $socialDrivers,
87 * Handle a registration request for the application.
88 * @throws UserRegistrationException
90 public function postRegister(Request $request)
92 $this->registrationService->ensureRegistrationAllowed();
93 $this->validator($request->all())->validate();
94 $userData = $request->all();
97 $user = $this->registrationService->registerUser($userData);
99 Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
100 $this->logActivity(ActivityType::AUTH_LOGIN, $user);
101 } catch (UserRegistrationException $exception) {
102 if ($exception->getMessage()) {
103 $this->showErrorNotification($exception->getMessage());
105 return redirect($exception->redirectLocation);
108 $this->showSuccessNotification(trans('auth.register_success'));
109 return redirect($this->redirectPath());
113 * Create a new user instance after a valid registration.
117 protected function create(array $data)
119 return User::create([
120 'name' => $data['name'],
121 'email' => $data['email'],
122 'password' => Hash::make($data['password']),