3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Http\Controllers\Controller;
6 use BookStack\Repos\UserRepo;
7 use BookStack\Services\SocialAuthService;
8 use Illuminate\Contracts\Auth\Authenticatable;
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;
28 * Where to redirect users after login.
32 protected $redirectTo = '/';
34 protected $redirectPath = '/';
35 protected $redirectAfterLogout = '/login';
37 protected $socialAuthService;
41 * Create a new controller instance.
43 * @param SocialAuthService $socialAuthService
44 * @param UserRepo $userRepo
46 public function __construct(SocialAuthService $socialAuthService, UserRepo $userRepo)
48 $this->middleware('guest', ['only' => ['getLogin', 'postLogin']]);
49 $this->socialAuthService = $socialAuthService;
50 $this->userRepo = $userRepo;
51 $this->redirectPath = baseUrl('/');
52 $this->redirectAfterLogout = baseUrl('/login');
53 parent::__construct();
56 public function username()
58 return config('auth.method') === 'standard' ? 'email' : 'username';
62 * Overrides the action when a user is authenticated.
63 * If the user authenticated but does not exist in the user table we create them.
64 * @param Request $request
65 * @param Authenticatable $user
66 * @return \Illuminate\Http\RedirectResponse
67 * @throws AuthException
69 protected function authenticated(Request $request, Authenticatable $user)
71 // Explicitly log them out for now if they do no exist.
72 if (!$user->exists) auth()->logout($user);
74 if (!$user->exists && $user->email === null && !$request->has('email')) {
76 session()->flash('request-email', true);
77 return redirect('/login');
80 if (!$user->exists && $user->email === null && $request->has('email')) {
81 $user->email = $request->get('email');
86 // Check for users with same email already
87 $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
89 throw new AuthException('A user with the email ' . $user->email . ' already exists but with different credentials.');
93 $this->userRepo->attachDefaultRole($user);
97 $path = session()->pull('url.intended', '/');
98 $path = baseUrl($path, true);
99 return redirect($path);
103 * Show the application login form.
104 * @return \Illuminate\Http\Response
106 public function getLogin()
108 $socialDrivers = $this->socialAuthService->getActiveDrivers();
109 $authMethod = config('auth.method');
110 return view('auth/login', ['socialDrivers' => $socialDrivers, 'authMethod' => $authMethod]);
114 * Redirect to the relevant social site.
115 * @param $socialDriver
116 * @return \Symfony\Component\HttpFoundation\RedirectResponse
118 public function getSocialLogin($socialDriver)
120 session()->put('social-callback', 'login');
121 return $this->socialAuthService->startLogIn($socialDriver);