3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\SocialAuthService;
6 use BookStack\Exceptions\LoginAttemptEmailNeededException;
7 use BookStack\Exceptions\LoginAttemptException;
8 use BookStack\Exceptions\UserRegistrationException;
9 use BookStack\Http\Controllers\Controller;
10 use Illuminate\Foundation\Auth\AuthenticatesUsers;
11 use Illuminate\Http\Request;
13 class LoginController extends Controller
16 |--------------------------------------------------------------------------
18 |--------------------------------------------------------------------------
20 | This controller handles authenticating users for the application and
21 | redirecting them to your home screen. The controller uses a trait
22 | to conveniently provide its functionality to your applications.
26 use AuthenticatesUsers;
31 protected $redirectTo = '/';
32 protected $redirectPath = '/';
33 protected $redirectAfterLogout = '/login';
35 protected $socialAuthService;
38 * Create a new controller instance.
40 public function __construct(SocialAuthService $socialAuthService)
42 $this->middleware('guest', ['only' => ['getLogin', 'login']]);
43 $this->middleware('guard:standard,ldap', ['only' => ['login', 'logout']]);
45 $this->socialAuthService = $socialAuthService;
46 $this->redirectPath = url('/');
47 $this->redirectAfterLogout = url('/login');
48 parent::__construct();
51 public function username()
53 return config('auth.method') === 'standard' ? 'email' : 'username';
57 * Get the needed authorization credentials from the request.
59 protected function credentials(Request $request)
61 return $request->only('username', 'email', 'password');
65 * Show the application login form.
67 public function getLogin(Request $request)
69 $socialDrivers = $this->socialAuthService->getActiveDrivers();
70 $authMethod = config('auth.method');
72 if ($request->has('email')) {
73 session()->flashInput([
74 'email' => $request->get('email'),
75 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
79 $previous = url()->previous('');
80 if (setting('app-public') && $previous && $previous !== url('/login')) {
81 redirect()->setIntendedUrl($previous);
84 return view('auth.login', [
85 'socialDrivers' => $socialDrivers,
86 'authMethod' => $authMethod,
91 * Handle a login request to the application.
93 * @param \Illuminate\Http\Request $request
94 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
96 * @throws \Illuminate\Validation\ValidationException
98 public function login(Request $request)
100 $this->validateLogin($request);
102 // If the class is using the ThrottlesLogins trait, we can automatically throttle
103 // the login attempts for this application. We'll key this by the username and
104 // the IP address of the client making these requests into this application.
105 if (method_exists($this, 'hasTooManyLoginAttempts') &&
106 $this->hasTooManyLoginAttempts($request)) {
107 $this->fireLockoutEvent($request);
109 return $this->sendLockoutResponse($request);
113 if ($this->attemptLogin($request)) {
114 return $this->sendLoginResponse($request);
116 } catch (LoginAttemptException $exception) {
117 return $this->sendLoginAttemptExceptionResponse($exception, $request);
120 // If the login attempt was unsuccessful we will increment the number of attempts
121 // to login and redirect the user back to the login form. Of course, when this
122 // user surpasses their maximum number of attempts they will get locked out.
123 $this->incrementLoginAttempts($request);
125 return $this->sendFailedLoginResponse($request);
129 * The user has been authenticated.
131 * @param \Illuminate\Http\Request $request
135 protected function authenticated(Request $request, $user)
137 // Authenticate on all session guards if a likely admin
138 if ($user->can('users-manage') && $user->can('user-roles-manage')) {
139 $guards = ['standard', 'ldap', 'saml2', 'openid'];
140 foreach ($guards as $guard) {
141 auth($guard)->login($user);
145 return redirect()->intended($this->redirectPath());
149 * Validate the user login request.
151 * @param \Illuminate\Http\Request $request
154 * @throws \Illuminate\Validation\ValidationException
156 protected function validateLogin(Request $request)
158 $rules = ['password' => 'required|string'];
159 $authMethod = config('auth.method');
161 if ($authMethod === 'standard') {
162 $rules['email'] = 'required|email';
165 if ($authMethod === 'ldap') {
166 $rules['username'] = 'required|string';
167 $rules['email'] = 'email';
170 $request->validate($rules);
174 * Send a response when a login attempt exception occurs.
176 protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
178 if ($exception instanceof LoginAttemptEmailNeededException) {
180 session()->flash('request-email', true);
183 if ($message = $exception->getMessage()) {
184 $this->showWarningNotification($message);
187 return redirect('/login');