3 namespace BookStack\Access\Controllers;
5 use BookStack\Access\LoginService;
6 use BookStack\Access\RegistrationService;
7 use BookStack\Access\SocialAuthService;
8 use BookStack\Exceptions\SocialDriverNotConfigured;
9 use BookStack\Exceptions\SocialSignInAccountNotUsed;
10 use BookStack\Exceptions\SocialSignInException;
11 use BookStack\Exceptions\UserRegistrationException;
12 use BookStack\Http\Controller;
13 use Illuminate\Http\Request;
14 use Illuminate\Support\Str;
15 use Laravel\Socialite\Contracts\User as SocialUser;
17 class SocialController extends Controller
19 public function __construct(
20 protected SocialAuthService $socialAuthService,
21 protected RegistrationService $registrationService,
22 protected LoginService $loginService,
24 $this->middleware('guest')->only(['register']);
28 * Redirect to the relevant social site.
30 * @throws SocialDriverNotConfigured
32 public function login(string $socialDriver)
34 session()->put('social-callback', 'login');
36 return $this->socialAuthService->startLogIn($socialDriver);
40 * Redirect to the social site for authentication intended to register.
42 * @throws SocialDriverNotConfigured
43 * @throws UserRegistrationException
45 public function register(string $socialDriver)
47 $this->registrationService->ensureRegistrationAllowed();
48 session()->put('social-callback', 'register');
50 return $this->socialAuthService->startRegister($socialDriver);
54 * The callback for social login services.
56 * @throws SocialSignInException
57 * @throws SocialDriverNotConfigured
58 * @throws UserRegistrationException
60 public function callback(Request $request, string $socialDriver)
62 if (!session()->has('social-callback')) {
63 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
66 // Check request for error information
67 if ($request->has('error') && $request->has('error_description')) {
68 throw new SocialSignInException(trans('errors.social_login_bad_response', [
69 'socialAccount' => $socialDriver,
70 'error' => $request->get('error_description'),
74 $action = session()->pull('social-callback');
76 // Attempt login or fall-back to register if allowed.
77 $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
78 if ($action === 'login') {
80 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
81 } catch (SocialSignInAccountNotUsed $exception) {
82 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
83 return $this->socialRegisterCallback($socialDriver, $socialUser);
90 if ($action === 'register') {
91 return $this->socialRegisterCallback($socialDriver, $socialUser);
94 return redirect()->back();
98 * Detach a social account from a user.
100 public function detach(string $socialDriver)
102 $this->socialAuthService->detachSocialAccount($socialDriver);
103 session()->flash('success', trans('settings.users_social_disconnected', ['socialAccount' => Str::title($socialDriver)]));
105 return redirect('/my-account/auth#social-accounts');
109 * Register a new user after a registration callback.
111 * @throws UserRegistrationException
113 protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
115 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
116 $socialAccount = $this->socialAuthService->newSocialAccount($socialDriver, $socialUser);
117 $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver);
119 // Create an array of the user data to create a new user instance
121 'name' => $socialUser->getName(),
122 'email' => $socialUser->getEmail(),
123 'password' => Str::random(32),
126 // Take name from email address if empty
127 if (!$userData['name']) {
128 $userData['name'] = explode('@', $userData['email'])[0];
131 $user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified);
132 $this->showSuccessNotification(trans('auth.register_success'));
133 $this->loginService->login($user, $socialDriver);
135 return redirect('/');