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\Services\SocialAuthService;
9 use Illuminate\Contracts\Auth\Authenticatable;
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;
29 * Where to redirect users after login.
33 protected $redirectTo = '/';
35 protected $redirectPath = '/';
36 protected $redirectAfterLogout = '/login';
38 protected $socialAuthService;
42 * Create a new controller instance.
44 * @param SocialAuthService $socialAuthService
45 * @param UserRepo $userRepo
47 public function __construct(SocialAuthService $socialAuthService, UserRepo $userRepo)
49 $this->middleware('guest', ['only' => ['getLogin', 'postLogin']]);
50 $this->socialAuthService = $socialAuthService;
51 $this->userRepo = $userRepo;
52 $this->redirectPath = baseUrl('/');
53 $this->redirectAfterLogout = baseUrl('/login');
54 parent::__construct();
57 public function username()
59 return config('auth.method') === 'standard' ? 'email' : 'username';
63 * Overrides the action when a user is authenticated.
64 * If the user authenticated but does not exist in the user table we create them.
65 * @param Request $request
66 * @param Authenticatable $user
67 * @return \Illuminate\Http\RedirectResponse
68 * @throws AuthException
70 protected function authenticated(Request $request, Authenticatable $user)
72 // Explicitly log them out for now if they do no exist.
74 auth()->logout($user);
77 if (!$user->exists && $user->email === null && !$request->filled('email')) {
79 session()->flash('request-email', true);
80 return redirect('/login');
83 if (!$user->exists && $user->email === null && $request->filled('email')) {
84 $user->email = $request->get('email');
88 // Check for users with same email already
89 $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
91 throw new AuthException(trans('errors.error_user_exists_different_creds', ['email' => $user->email]));
95 $this->userRepo->attachDefaultRole($user);
99 $path = session()->pull('url.intended', '/');
100 $path = baseUrl($path, true);
101 return redirect($path);
105 * Show the application login form.
106 * @param Request $request
107 * @return \Illuminate\Http\Response
109 public function getLogin(Request $request)
111 $socialDrivers = $this->socialAuthService->getActiveDrivers();
112 $authMethod = config('auth.method');
114 if ($request->has('email')) {
115 session()->flashInput([
116 'email' => $request->get('email'),
117 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
121 return view('auth/login', ['socialDrivers' => $socialDrivers, 'authMethod' => $authMethod]);
125 * Redirect to the relevant social site.
126 * @param $socialDriver
127 * @return \Symfony\Component\HttpFoundation\RedirectResponse
129 public function getSocialLogin($socialDriver)
131 session()->put('social-callback', 'login');
132 return $this->socialAuthService->startLogIn($socialDriver);