3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\RegistrationService;
7 use BookStack\Auth\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\Facades\Theme;
13 use BookStack\Http\Controllers\Controller;
14 use BookStack\Theming\ThemeEvents;
15 use Illuminate\Http\Request;
16 use Illuminate\Support\Str;
17 use Laravel\Socialite\Contracts\User as SocialUser;
19 class SocialController extends Controller
22 protected $socialAuthService;
23 protected $registrationService;
26 * SocialController constructor.
28 public function __construct(SocialAuthService $socialAuthService, RegistrationService $registrationService)
30 $this->middleware('guest')->only(['getRegister', 'postRegister']);
31 $this->socialAuthService = $socialAuthService;
32 $this->registrationService = $registrationService;
36 * Redirect to the relevant social site.
37 * @throws SocialDriverNotConfigured
39 public function login(string $socialDriver)
41 session()->put('social-callback', 'login');
42 return $this->socialAuthService->startLogIn($socialDriver);
46 * Redirect to the social site for authentication intended to register.
47 * @throws SocialDriverNotConfigured
48 * @throws UserRegistrationException
50 public function register(string $socialDriver)
52 $this->registrationService->ensureRegistrationAllowed();
53 session()->put('social-callback', 'register');
54 return $this->socialAuthService->startRegister($socialDriver);
58 * The callback for social login services.
59 * @throws SocialSignInException
60 * @throws SocialDriverNotConfigured
61 * @throws UserRegistrationException
63 public function callback(Request $request, string $socialDriver)
65 if (!session()->has('social-callback')) {
66 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
69 // Check request for error information
70 if ($request->has('error') && $request->has('error_description')) {
71 throw new SocialSignInException(trans('errors.social_login_bad_response', [
72 'socialAccount' => $socialDriver,
73 'error' => $request->get('error_description'),
77 $action = session()->pull('social-callback');
79 // Attempt login or fall-back to register if allowed.
80 $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
81 if ($action === 'login') {
83 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
84 } catch (SocialSignInAccountNotUsed $exception) {
85 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
86 return $this->socialRegisterCallback($socialDriver, $socialUser);
92 if ($action === 'register') {
93 return $this->socialRegisterCallback($socialDriver, $socialUser);
96 return redirect()->back();
100 * Detach a social account from a user.
102 public function detach(string $socialDriver)
104 $this->socialAuthService->detachSocialAccount($socialDriver);
105 session()->flash('success', trans('settings.users_social_disconnected', ['socialAccount' => Str::title($socialDriver)]));
106 return redirect(user()->getEditUrl());
110 * 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 auth()->login($user);
133 Theme::dispatch(ThemeEvents::AUTH_LOGIN, $socialDriver, $user);
134 $this->logActivity(ActivityType::AUTH_LOGIN, $user);
136 $this->showSuccessNotification(trans('auth.register_success'));
137 return redirect('/');