33namespace CodeIgniter \Shield \Controllers ;
44
55use App \Controllers \BaseController ;
6- use CodeIgniter \HTTP \IncomingRequest ;
76use CodeIgniter \HTTP \RedirectResponse ;
8- use CodeIgniter \HTTP \Response ;
97
108class LoginController extends BaseController
119{
@@ -27,18 +25,21 @@ public function loginView()
2725
2826 /**
2927 * Attempts to log the user in.
30- *
31- * @return Response|string
3228 */
33- public function loginAction ()
29+ public function loginAction (): RedirectResponse
3430 {
35- /** @var IncomingRequest $request */
36- $ request = service ('request ' );
31+ // Validate here first, since some things,
32+ // like the password, can only be validated properly here.
33+ $ rules = $ this ->getValidationRules ();
34+
35+ if (! $ this ->validate ($ rules )) {
36+ return redirect ()->back ()->withInput ()->with ('errors ' , $ this ->validator ->getErrors ());
37+ }
3738
38- $ credentials = $ request ->getPost (setting ('Auth.validFields ' ));
39+ $ credentials = $ this -> request ->getPost (setting ('Auth.validFields ' ));
3940 $ credentials = array_filter ($ credentials );
40- $ credentials ['password ' ] = $ request ->getPost ('password ' );
41- $ remember = (bool ) $ request ->getPost ('remember ' );
41+ $ credentials ['password ' ] = $ this -> request ->getPost ('password ' );
42+ $ remember = (bool ) $ this -> request ->getPost ('remember ' );
4243
4344 // Attempt to login
4445 $ result = auth ('session ' )->remember ($ remember )->attempt ($ credentials );
@@ -56,11 +57,23 @@ public function loginAction()
5657 }
5758
5859 /**
59- * Logs the current user out .
60+ * Returns the rules that should be used for validation .
6061 *
61- * @return Response|string
62+ * @return string[]
63+ */
64+ protected function getValidationRules (): array
65+ {
66+ return setting ('Validation.login ' ) ?? [
67+ //'username' => 'required|max_length[30]|alpha_numeric_space|min_length[3]',
68+ 'email ' => 'required|max_length[254]|valid_email ' ,
69+ 'password ' => 'required ' ,
70+ ];
71+ }
72+
73+ /**
74+ * Logs the current user out.
6275 */
63- public function logoutAction ()
76+ public function logoutAction (): RedirectResponse
6477 {
6578 auth ()->logout ();
6679
0 commit comments