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 return view('auth.login', [
80 'socialDrivers' => $socialDrivers,
81 'authMethod' => $authMethod,
86 * Handle a login request to the application.
88 * @param \Illuminate\Http\Request $request
89 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
91 * @throws \Illuminate\Validation\ValidationException
93 public function login(Request $request)
95 $this->validateLogin($request);
97 // If the class is using the ThrottlesLogins trait, we can automatically throttle
98 // the login attempts for this application. We'll key this by the username and
99 // the IP address of the client making these requests into this application.
100 if (method_exists($this, 'hasTooManyLoginAttempts') &&
101 $this->hasTooManyLoginAttempts($request)) {
102 $this->fireLockoutEvent($request);
104 return $this->sendLockoutResponse($request);
108 if ($this->attemptLogin($request)) {
109 return $this->sendLoginResponse($request);
111 } catch (LoginAttemptException $exception) {
112 return $this->sendLoginAttemptExceptionResponse($exception, $request);
115 // If the login attempt was unsuccessful we will increment the number of attempts
116 // to login and redirect the user back to the login form. Of course, when this
117 // user surpasses their maximum number of attempts they will get locked out.
118 $this->incrementLoginAttempts($request);
120 return $this->sendFailedLoginResponse($request);
124 * Validate the user login request.
126 * @param \Illuminate\Http\Request $request
129 * @throws \Illuminate\Validation\ValidationException
131 protected function validateLogin(Request $request)
133 $rules = ['password' => 'required|string'];
134 $authMethod = config('auth.method');
136 if ($authMethod === 'standard') {
137 $rules['email'] = 'required|email';
140 if ($authMethod === 'ldap') {
141 $rules['username'] = 'required|string';
142 $rules['email'] = 'email';
145 $request->validate($rules);
149 * Send a response when a login attempt exception occurs.
151 protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
153 if ($exception instanceof LoginAttemptEmailNeededException) {
155 session()->flash('request-email', true);
158 if ($message = $exception->getMessage()) {
159 $this->showWarningNotification($message);
162 return redirect('/login');