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
21 protected $socialAuthService;
22 protected $registrationService;
25 * SocialController constructor.
27 public function __construct(SocialAuthService $socialAuthService, RegistrationService $registrationService)
29 $this->middleware('guest')->only(['getRegister', 'postRegister']);
30 $this->socialAuthService = $socialAuthService;
31 $this->registrationService = $registrationService;
35 * Redirect to the relevant social site.
37 * @throws SocialDriverNotConfigured
39 public function login(string $socialDriver)
41 session()->put('social-callback', 'login');
43 return $this->socialAuthService->startLogIn($socialDriver);
47 * Redirect to the social site for authentication intended to register.
49 * @throws SocialDriverNotConfigured
50 * @throws UserRegistrationException
52 public function register(string $socialDriver)
54 $this->registrationService->ensureRegistrationAllowed();
55 session()->put('social-callback', 'register');
57 return $this->socialAuthService->startRegister($socialDriver);
61 * The callback for social login services.
63 * @throws SocialSignInException
64 * @throws SocialDriverNotConfigured
65 * @throws UserRegistrationException
67 public function callback(Request $request, string $socialDriver)
69 if (!session()->has('social-callback')) {
70 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
73 // Check request for error information
74 if ($request->has('error') && $request->has('error_description')) {
75 throw new SocialSignInException(trans('errors.social_login_bad_response', [
76 'socialAccount' => $socialDriver,
77 'error' => $request->get('error_description'),
81 $action = session()->pull('social-callback');
83 // Attempt login or fall-back to register if allowed.
84 $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
85 if ($action === 'login') {
87 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
88 } catch (SocialSignInAccountNotUsed $exception) {
89 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
90 return $this->socialRegisterCallback($socialDriver, $socialUser);
97 if ($action === 'register') {
98 return $this->socialRegisterCallback($socialDriver, $socialUser);
101 return redirect()->back();
105 * Detach a social account from a user.
107 public function detach(string $socialDriver)
109 $this->socialAuthService->detachSocialAccount($socialDriver);
110 session()->flash('success', trans('settings.users_social_disconnected', ['socialAccount' => Str::title($socialDriver)]));
112 return redirect(user()->getEditUrl());
116 * Register a new user after a registration callback.
118 * @throws UserRegistrationException
120 protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
122 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
123 $socialAccount = $this->socialAuthService->newSocialAccount($socialDriver, $socialUser);
124 $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver);
126 // Create an array of the user data to create a new user instance
128 'name' => $socialUser->getName(),
129 'email' => $socialUser->getEmail(),
130 'password' => Str::random(32),
133 // Take name from email address if empty
134 if (!$userData['name']) {
135 $userData['name'] = explode('@', $userData['email'])[0];
138 $user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified);
139 auth()->login($user);
140 Theme::dispatch(ThemeEvents::AUTH_LOGIN, $socialDriver, $user);
141 $this->logActivity(ActivityType::AUTH_LOGIN, $user);
143 $this->showSuccessNotification(trans('auth.register_success'));
145 return redirect('/');