22
33namespace BookStack \Http \Controllers \Auth ;
44
5+ use Illuminate \Contracts \Auth \Authenticatable ;
56use Illuminate \Http \Request ;
67use BookStack \Exceptions \SocialSignInException ;
78use BookStack \Exceptions \UserRegistrationException ;
@@ -29,9 +30,10 @@ class AuthController extends Controller
2930
3031 use AuthenticatesAndRegistersUsers, ThrottlesLogins;
3132
32- protected $ loginPath = '/login ' ;
3333 protected $ redirectPath = '/ ' ;
3434 protected $ redirectAfterLogout = '/login ' ;
35+ protected $ username = 'email ' ;
36+
3537
3638 protected $ socialAuthService ;
3739 protected $ emailConfirmationService ;
@@ -49,6 +51,7 @@ public function __construct(SocialAuthService $socialAuthService, EmailConfirmat
4951 $ this ->socialAuthService = $ socialAuthService ;
5052 $ this ->emailConfirmationService = $ emailConfirmationService ;
5153 $ this ->userRepo = $ userRepo ;
54+ $ this ->username = config ('auth.method ' ) === 'standard ' ? 'email ' : 'username ' ;
5255 parent ::__construct ();
5356 }
5457
@@ -105,6 +108,38 @@ public function postRegister(Request $request)
105108 return $ this ->registerUser ($ userData );
106109 }
107110
111+
112+ /**
113+ * Overrides the action when a user is authenticated.
114+ * If the user authenticated but does not exist in the user table we create them.
115+ * @param Request $request
116+ * @param Authenticatable $user
117+ * @return \Illuminate\Http\RedirectResponse
118+ */
119+ protected function authenticated (Request $ request , Authenticatable $ user )
120+ {
121+ // Explicitly log them out for now if they do no exist.
122+ if (!$ user ->exists ) auth ()->logout ($ user );
123+
124+ if (!$ user ->exists && $ user ->email === null && !$ request ->has ('email ' )) {
125+ $ request ->flash ();
126+ session ()->flash ('request-email ' , true );
127+ return redirect ('/login ' );
128+ }
129+
130+ if (!$ user ->exists && $ user ->email === null && $ request ->has ('email ' )) {
131+ $ user ->email = $ request ->get ('email ' );
132+ }
133+
134+ if (!$ user ->exists ) {
135+ $ user ->save ();
136+ $ this ->userRepo ->attachDefaultRole ($ user );
137+ auth ()->login ($ user );
138+ }
139+
140+ return redirect ()->intended ($ this ->redirectPath ());
141+ }
142+
108143 /**
109144 * Register a new user after a registration callback.
110145 * @param $socialDriver
@@ -156,13 +191,14 @@ protected function registerUser(array $userData, $socialAccount = false)
156191 }
157192
158193 $ newUser ->email_confirmed = true ;
194+
159195 auth ()->login ($ newUser );
160196 session ()->flash ('success ' , 'Thanks for signing up! You are now registered and signed in. ' );
161197 return redirect ($ this ->redirectPath ());
162198 }
163199
164200 /**
165- * Show the page to tell the user to check thier email
201+ * Show the page to tell the user to check their email
166202 * and confirm their address.
167203 */
168204 public function getRegisterConfirmation ()
@@ -222,7 +258,7 @@ public function resendConfirmation(Request $request)
222258 ]);
223259 $ user = $ this ->userRepo ->getByEmail ($ request ->get ('email ' ));
224260 $ this ->emailConfirmationService ->sendConfirmation ($ user );
225- \Session:: flash ('success ' , 'Confirmation email resent, Please check your inbox. ' );
261+ session ()-> flash ('success ' , 'Confirmation email resent, Please check your inbox. ' );
226262 return redirect ('/register/confirm ' );
227263 }
228264
@@ -232,13 +268,9 @@ public function resendConfirmation(Request $request)
232268 */
233269 public function getLogin ()
234270 {
235-
236- if (view ()->exists ('auth.authenticate ' )) {
237- return view ('auth.authenticate ' );
238- }
239-
240271 $ socialDrivers = $ this ->socialAuthService ->getActiveDrivers ();
241- return view ('auth.login ' , ['socialDrivers ' => $ socialDrivers ]);
272+ $ authMethod = config ('auth.method ' );
273+ return view ('auth/login ' , ['socialDrivers ' => $ socialDrivers , 'authMethod ' => $ authMethod ]);
242274 }
243275
244276 /**
@@ -253,7 +285,7 @@ public function getSocialLogin($socialDriver)
253285 }
254286
255287 /**
256- * Redirect to the social site for authentication initended to register.
288+ * Redirect to the social site for authentication intended to register.
257289 * @param $socialDriver
258290 * @return mixed
259291 */
0 commit comments