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\Http\Controllers\Controller;
9 use Illuminate\Foundation\Auth\AuthenticatesUsers;
10 use Illuminate\Http\Request;
12 class LoginController extends Controller
15 |--------------------------------------------------------------------------
17 |--------------------------------------------------------------------------
19 | This controller handles authenticating users for the application and
20 | redirecting them to your home screen. The controller uses a trait
21 | to conveniently provide its functionality to your applications.
25 use AuthenticatesUsers;
30 protected $redirectTo = '/';
31 protected $redirectPath = '/';
32 protected $redirectAfterLogout = '/login';
34 protected $socialAuthService;
37 * Create a new controller instance.
39 public function __construct(SocialAuthService $socialAuthService)
41 $this->middleware('guest', ['only' => ['getLogin', 'login']]);
42 $this->middleware('guard:standard,ldap', ['only' => ['login', 'logout']]);
44 $this->socialAuthService = $socialAuthService;
45 $this->redirectPath = url('/');
46 $this->redirectAfterLogout = url('/login');
47 parent::__construct();
50 public function username()
52 return config('auth.method') === 'standard' ? 'email' : 'username';
56 * Get the needed authorization credentials from the request.
58 protected function credentials(Request $request)
60 return $request->only('username', 'email', 'password');
64 * Show the application login form.
66 public function getLogin(Request $request)
68 $socialDrivers = $this->socialAuthService->getActiveDrivers();
69 $authMethod = config('auth.method');
71 if ($request->has('email')) {
72 session()->flashInput([
73 'email' => $request->get('email'),
74 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
78 return view('auth.login', [
79 'socialDrivers' => $socialDrivers,
80 'authMethod' => $authMethod,
85 * Handle a login request to the application.
87 * @param \Illuminate\Http\Request $request
88 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
90 * @throws \Illuminate\Validation\ValidationException
92 public function login(Request $request)
94 $this->validateLogin($request);
96 // If the class is using the ThrottlesLogins trait, we can automatically throttle
97 // the login attempts for this application. We'll key this by the username and
98 // the IP address of the client making these requests into this application.
99 if (method_exists($this, 'hasTooManyLoginAttempts') &&
100 $this->hasTooManyLoginAttempts($request)) {
101 $this->fireLockoutEvent($request);
103 return $this->sendLockoutResponse($request);
107 if ($this->attemptLogin($request)) {
108 return $this->sendLoginResponse($request);
110 } catch (LoginAttemptException $exception) {
111 return $this->sendLoginAttemptExceptionResponse($exception, $request);
114 // If the login attempt was unsuccessful we will increment the number of attempts
115 // to login and redirect the user back to the login form. Of course, when this
116 // user surpasses their maximum number of attempts they will get locked out.
117 $this->incrementLoginAttempts($request);
119 return $this->sendFailedLoginResponse($request);
123 * Validate the user login request.
125 * @param \Illuminate\Http\Request $request
128 * @throws \Illuminate\Validation\ValidationException
130 protected function validateLogin(Request $request)
132 $rules = ['password' => 'required|string'];
133 $authMethod = config('auth.method');
135 if ($authMethod === 'standard') {
136 $rules['email'] = 'required|email';
139 if ($authMethod === 'ldap') {
140 $rules['username'] = 'required|string';
141 $rules['email'] = 'email';
144 $request->validate($rules);
148 * Send a response when a login attempt exception occurs.
150 protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
152 if ($exception instanceof LoginAttemptEmailNeededException) {
154 session()->flash('request-email', true);
157 if ($message = $exception->getMessage()) {
158 $this->showWarningNotification($message);
161 return redirect('/login');