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.
73 if (!$user->exists) auth()->logout($user);
75 if (!$user->exists && $user->email === null && !$request->has('email')) {
77 session()->flash('request-email', true);
78 return redirect('/login');
81 if (!$user->exists && $user->email === null && $request->has('email')) {
82 $user->email = $request->get('email');
87 // Check for users with same email already
88 $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
90 throw new AuthException(trans('errors.error_user_exists_different_creds', ['email' => $user->email]));
94 $this->userRepo->attachDefaultRole($user);
98 $path = session()->pull('url.intended', '/');
99 $path = baseUrl($path, true);
100 return redirect($path);
104 * Show the application login form.
105 * @return \Illuminate\Http\Response
107 public function getLogin()
109 $socialDrivers = $this->socialAuthService->getActiveDrivers();
110 $authMethod = config('auth.method');
111 return view('auth/login', ['socialDrivers' => $socialDrivers, 'authMethod' => $authMethod]);
115 * Redirect to the relevant social site.
116 * @param $socialDriver
117 * @return \Symfony\Component\HttpFoundation\RedirectResponse
119 public function getSocialLogin($socialDriver)
121 session()->put('social-callback', 'login');
122 return $this->socialAuthService->startLogIn($socialDriver);