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 if ($request->has('intended')) {
80 redirect()->setIntendedUrl($request->get('intended'));
83 return view('auth.login', [
84 'socialDrivers' => $socialDrivers,
85 'authMethod' => $authMethod,
90 * Handle a login request to the application.
92 * @param \Illuminate\Http\Request $request
93 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
95 * @throws \Illuminate\Validation\ValidationException
97 public function login(Request $request)
99 $this->validateLogin($request);
101 // If the class is using the ThrottlesLogins trait, we can automatically throttle
102 // the login attempts for this application. We'll key this by the username and
103 // the IP address of the client making these requests into this application.
104 if (method_exists($this, 'hasTooManyLoginAttempts') &&
105 $this->hasTooManyLoginAttempts($request)) {
106 $this->fireLockoutEvent($request);
108 return $this->sendLockoutResponse($request);
112 if ($this->attemptLogin($request)) {
113 return $this->sendLoginResponse($request);
115 } catch (LoginAttemptException $exception) {
116 return $this->sendLoginAttemptExceptionResponse($exception, $request);
119 // If the login attempt was unsuccessful we will increment the number of attempts
120 // to login and redirect the user back to the login form. Of course, when this
121 // user surpasses their maximum number of attempts they will get locked out.
122 $this->incrementLoginAttempts($request);
124 return $this->sendFailedLoginResponse($request);
128 * Validate the user login request.
130 * @param \Illuminate\Http\Request $request
133 * @throws \Illuminate\Validation\ValidationException
135 protected function validateLogin(Request $request)
137 $rules = ['password' => 'required|string'];
138 $authMethod = config('auth.method');
140 if ($authMethod === 'standard') {
141 $rules['email'] = 'required|email';
144 if ($authMethod === 'ldap') {
145 $rules['username'] = 'required|string';
146 $rules['email'] = 'email';
149 $request->validate($rules);
153 * Send a response when a login attempt exception occurs.
155 protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
157 if ($exception instanceof LoginAttemptEmailNeededException) {
159 session()->flash('request-email', true);
162 if ($message = $exception->getMessage()) {
163 $this->showWarningNotification($message);
166 return redirect('/login');