]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/RegisterController.php
Updated auth controllers with property types
[bookstack] / app / Http / Controllers / Auth / RegisterController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
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;
15 use Illuminate\Support\Facades\Validator;
16 use Illuminate\Validation\Rules\Password;
17
18 class RegisterController extends Controller
19 {
20     /*
21     |--------------------------------------------------------------------------
22     | Register Controller
23     |--------------------------------------------------------------------------
24     |
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.
28     |
29     */
30     use RegistersUsers;
31
32     protected SocialAuthService $socialAuthService;
33     protected RegistrationService $registrationService;
34     protected LoginService $loginService;
35
36     /**
37      * Where to redirect users after login / registration.
38      */
39     protected string $redirectTo = '/';
40     protected string $redirectPath = '/';
41
42     /**
43      * Create a new controller instance.
44      */
45     public function __construct(
46         SocialAuthService $socialAuthService,
47         RegistrationService $registrationService,
48         LoginService $loginService
49     ) {
50         $this->middleware('guest');
51         $this->middleware('guard:standard');
52
53         $this->socialAuthService = $socialAuthService;
54         $this->registrationService = $registrationService;
55         $this->loginService = $loginService;
56
57         $this->redirectTo = url('/');
58         $this->redirectPath = url('/');
59     }
60
61     /**
62      * Get a validator for an incoming registration request.
63      *
64      * @return \Illuminate\Contracts\Validation\Validator
65      */
66     protected function validator(array $data)
67     {
68         return Validator::make($data, [
69             'name'     => ['required', 'min:2', 'max:100'],
70             'email'    => ['required', 'email', 'max:255', 'unique:users'],
71             'password' => ['required', Password::default()],
72         ]);
73     }
74
75     /**
76      * Show the application registration form.
77      *
78      * @throws UserRegistrationException
79      */
80     public function getRegister()
81     {
82         $this->registrationService->ensureRegistrationAllowed();
83         $socialDrivers = $this->socialAuthService->getActiveDrivers();
84
85         return view('auth.register', [
86             'socialDrivers' => $socialDrivers,
87         ]);
88     }
89
90     /**
91      * Handle a registration request for the application.
92      *
93      * @throws UserRegistrationException
94      * @throws StoppedAuthenticationException
95      */
96     public function postRegister(Request $request)
97     {
98         $this->registrationService->ensureRegistrationAllowed();
99         $this->validator($request->all())->validate();
100         $userData = $request->all();
101
102         try {
103             $user = $this->registrationService->registerUser($userData);
104             $this->loginService->login($user, auth()->getDefaultDriver());
105         } catch (UserRegistrationException $exception) {
106             if ($exception->getMessage()) {
107                 $this->showErrorNotification($exception->getMessage());
108             }
109
110             return redirect($exception->redirectLocation);
111         }
112
113         $this->showSuccessNotification(trans('auth.register_success'));
114
115         return redirect($this->redirectPath());
116     }
117
118     /**
119      * Create a new user instance after a valid registration.
120      *
121      * @param array $data
122      *
123      * @return User
124      */
125     protected function create(array $data)
126     {
127         return User::create([
128             'name'     => $data['name'],
129             'email'    => $data['email'],
130             'password' => Hash::make($data['password']),
131         ]);
132     }
133 }