3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Exceptions\AuthException;
6 use BookStack\Http\Controllers\Controller;
7 use BookStack\Repos\UserRepo;
8 use BookStack\Repos\LdapRepo;
9 use BookStack\Services\SocialAuthService;
10 use Illuminate\Contracts\Auth\Authenticatable;
11 use Illuminate\Foundation\Auth\AuthenticatesUsers;
12 use Illuminate\Http\Request;
14 class LoginController extends Controller
17 |--------------------------------------------------------------------------
19 |--------------------------------------------------------------------------
21 | This controller handles authenticating users for the application and
22 | redirecting them to your home screen. The controller uses a trait
23 | to conveniently provide its functionality to your applications.
27 use AuthenticatesUsers;
30 * Where to redirect users after login.
34 protected $redirectTo = '/';
36 protected $redirectPath = '/';
37 protected $redirectAfterLogout = '/login';
39 protected $socialAuthService;
43 * Create a new controller instance.
45 * @param SocialAuthService $socialAuthService
46 * @param UserRepo $userRepo
48 public function __construct(SocialAuthService $socialAuthService, UserRepo $userRepo)
50 $this->middleware('guest', ['only' => ['getLogin', 'postLogin']]);
51 $this->socialAuthService = $socialAuthService;
52 $this->userRepo = $userRepo;
53 $this->redirectPath = baseUrl('/');
54 $this->redirectAfterLogout = baseUrl('/login');
55 parent::__construct();
58 public function username()
60 return config('auth.method') === 'standard' ? 'email' : 'username';
64 * Overrides the action when a user is authenticated.
65 * If the user authenticated but does not exist in the user table we create them.
66 * @param Request $request
67 * @param Authenticatable $user
68 * @return \Illuminate\Http\RedirectResponse
69 * @throws AuthException
71 protected function authenticated(Request $request, Authenticatable $user)
73 // Explicitly log them out for now if they do no exist.
75 auth()->logout($user);
78 if (!$user->exists && $user->email === null && !$request->filled('email')) {
80 session()->flash('request-email', true);
81 return redirect('/login');
84 if (!$user->exists && $user->email === null && $request->filled('email')) {
85 $user->email = $request->get('email');
89 // Check for users with same email already
90 $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
92 throw new AuthException(trans('errors.error_user_exists_different_creds', ['email' => $user->email]));
96 $this->userRepo->attachDefaultRole($user);
100 // ldap groups refresh
101 if (config('services.ldap.user_to_groups') !== false && $request->filled('username')) {
102 $ldapRepo = new LdapRepo($this->userRepo);
103 $ldapRepo->syncGroups($user, $request->input('username'));
107 $path = session()->pull('url.intended', '/');
108 $path = baseUrl($path, true);
109 return redirect($path);
113 * Show the application login form.
114 * @param Request $request
115 * @return \Illuminate\Http\Response
117 public function getLogin(Request $request)
119 $socialDrivers = $this->socialAuthService->getActiveDrivers();
120 $authMethod = config('auth.method');
122 if ($request->has('email')) {
123 session()->flashInput([
124 'email' => $request->get('email'),
125 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
129 return view('auth/login', ['socialDrivers' => $socialDrivers, 'authMethod' => $authMethod]);
133 * Redirect to the relevant social site.
134 * @param $socialDriver
135 * @return \Symfony\Component\HttpFoundation\RedirectResponse
137 public function getSocialLogin($socialDriver)
139 session()->put('social-callback', 'login');
140 return $this->socialAuthService->startLogIn($socialDriver);