File tree Expand file tree Collapse file tree 4 files changed +82
-16
lines changed Expand file tree Collapse file tree 4 files changed +82
-16
lines changed Original file line number Diff line number Diff line change 1111use app \core \Application ;
1212use app \core \Controller ;
1313use app \core \Request ;
14+ use app \models \LoginForm ;
1415use app \models \User ;
1516
1617/**
@@ -28,10 +29,20 @@ public function home()
2829 ]);
2930 }
3031
31- public function login ()
32+ public function login (Request $ request )
3233 {
34+ $ loginForm = new LoginForm ();
35+ if ($ request ->getMethod () === 'post ' ) {
36+ $ loginForm ->loadData ($ request ->getBody ());
37+ if ($ loginForm ->validate () && $ loginForm ->login ()) {
38+ Application::$ app ->response ->redirect ('/ ' );
39+ return ;
40+ }
41+ }
3342 $ this ->setLayout ('auth ' );
34- return $ this ->render ('login ' );
43+ return $ this ->render ('login ' , [
44+ 'model ' => $ loginForm
45+ ]);
3546 }
3647
3748 public function register (Request $ request )
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * User: TheCodeholic
4+ * Date: 7/25/2020
5+ * Time: 9:36 AM
6+ */
7+
8+ namespace app \models ;
9+
10+
11+ use app \core \Model ;
12+
13+ /**
14+ * Class LoginForm
15+ *
16+ * @author Zura Sekhniashvili <[email protected] > 17+ * @package app\models
18+ */
19+ class LoginForm extends Model
20+ {
21+ public string $ email = '' ;
22+ public string $ password = '' ;
23+
24+ public function rules ()
25+ {
26+ return [
27+ 'email ' => [self ::RULE_REQUIRED ],
28+ 'password ' => [self ::RULE_REQUIRED ],
29+ ];
30+ }
31+
32+ public function labels ()
33+ {
34+ return [
35+ 'email ' => 'Your Email address ' ,
36+ 'password ' => 'Password '
37+ ];
38+ }
39+
40+ public function login ()
41+ {
42+ $ user = User::findOne (['email ' => $ this ->email ]);
43+ if (!$ user ) {
44+ $ this ->addError ('email ' , 'User does not exist with this email address ' );
45+ return false ;
46+ }
47+ if (!password_verify ($ this ->password , $ user ->password )) {
48+ $ this ->addError ('password ' , 'Password is incorrect ' );
49+ return false ;
50+ }
51+ return true ;
52+ }
53+ }
Original file line number Diff line number Diff line change 2727$ app ->router ->get ('/register ' , [SiteController::class, 'register ' ]);
2828$ app ->router ->post ('/register ' , [SiteController::class, 'register ' ]);
2929$ app ->router ->get ('/login ' , [SiteController::class, 'login ' ]);
30+ $ app ->router ->post ('/login ' , [SiteController::class, 'login ' ]);
3031$ app ->router ->get ('/contact ' , [SiteController::class, 'contact ' ]);
3132$ app ->router ->get ('/about ' , [AboutController::class, 'index ' ]);
3233
Original file line number Diff line number Diff line change 1- <h1 >Contact page </h1 >
2-
3- <form >
4- <div class = " form-group" >
5- <label >Email address</label >
6- <input type = " email" class = " form-control" name = " email" >
7- <small class = " form-text text-muted" >We'll never share your email with anyone else.</small >
8- </div >
9- <div class = " form-group" >
10- <label >Password</label >
11- <input type = " password" class = " form-control" name = " password" >
12- </div >
13- <button type = " submit" class = " btn btn-primary" >Submit</button >
14- </form >
1+ <?php
2+
3+ /** @var $model \app\models\LoginForm */
4+
5+ use app \core \form \Form ;
6+
7+ ?>
8+
9+ <h1>Login</h1>
10+
11+ <?php $ form = Form::begin ('' , 'post ' ) ?>
12+ <?php echo $ form ->field ($ model , 'email ' ) ?>
13+ <?php echo $ form ->field ($ model , 'password ' )->passwordField () ?>
14+ <button class="btn btn-success">Submit</button>
15+ <?php Form::end () ?>
You can’t perform that action at this time.
0 commit comments