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', 'postLogin']]);
42 $this->socialAuthService = $socialAuthService;
43 $this->redirectPath = url('/');
44 $this->redirectAfterLogout = url('/login');
45 parent::__construct();
48 public function username()
50 return config('auth.method') === 'standard' ? 'email' : 'username';
54 * Get the needed authorization credentials from the request.
56 protected function credentials(Request $request)
58 return $request->only('username', 'email', 'password');
62 * Show the application login form.
64 public function getLogin(Request $request)
66 $socialDrivers = $this->socialAuthService->getActiveDrivers();
67 $authMethod = config('auth.method');
69 if ($request->has('email')) {
70 session()->flashInput([
71 'email' => $request->get('email'),
72 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
76 return view('auth.login', [
77 'socialDrivers' => $socialDrivers,
78 'authMethod' => $authMethod,
83 * Handle a login request to the application.
85 * @param \Illuminate\Http\Request $request
86 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
88 * @throws \Illuminate\Validation\ValidationException
90 public function login(Request $request)
92 $this->validateLogin($request);
94 // If the class is using the ThrottlesLogins trait, we can automatically throttle
95 // the login attempts for this application. We'll key this by the username and
96 // the IP address of the client making these requests into this application.
97 if (method_exists($this, 'hasTooManyLoginAttempts') &&
98 $this->hasTooManyLoginAttempts($request)) {
99 $this->fireLockoutEvent($request);
101 return $this->sendLockoutResponse($request);
105 if ($this->attemptLogin($request)) {
106 return $this->sendLoginResponse($request);
108 } catch (LoginAttemptException $exception) {
109 return $this->sendLoginAttemptExceptionResponse($exception, $request);
112 // If the login attempt was unsuccessful we will increment the number of attempts
113 // to login and redirect the user back to the login form. Of course, when this
114 // user surpasses their maximum number of attempts they will get locked out.
115 $this->incrementLoginAttempts($request);
117 return $this->sendFailedLoginResponse($request);
121 * Validate the user login request.
123 * @param \Illuminate\Http\Request $request
126 * @throws \Illuminate\Validation\ValidationException
128 protected function validateLogin(Request $request)
130 $rules = ['password' => 'required|string'];
131 $authMethod = config('auth.method');
133 if ($authMethod === 'standard') {
134 $rules['email'] = 'required|email';
137 if ($authMethod === 'ldap') {
138 $rules['username'] = 'required|string';
139 $rules['email'] = 'email';
142 $request->validate($rules);
146 * Send a response when a login attempt exception occurs.
148 protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
150 if ($exception instanceof LoginAttemptEmailNeededException) {
152 session()->flash('request-email', true);
155 if ($message = $exception->getMessage()) {
156 $this->showWarningNotification($message);
159 return redirect('/login');
163 * Log the user out of the application.
165 public function logout(Request $request)
167 $this->guard()->logout();
168 $request->session()->invalidate();
170 return $this->loggedOut($request) ?: redirect('/');