1+ <?php
2+
3+ use Core \App ;
4+ use Core \Database ;
5+ use Core \Validator ;
6+
7+ $ email = $ _POST ['email ' ];
8+ $ password = $ _POST ['password ' ];
9+
10+
11+ // validate the form inputs.
12+ $ errors = [];
13+ if (!Validator::email ($ email )) {
14+ $ errors ['email ' ] = 'Please enter a valid email address. ' ;
15+ }
16+
17+ if (!Validator::string ($ password , 7 , 255 )) {
18+ $ errors ['password ' ] = 'Please provide a password with at least 7 characters. ' ;
19+ }
20+
21+ if (! empty ($ errors )) {
22+ return view ('registration/create.view.php ' , [
23+ 'errors ' => $ errors
24+ ]);
25+ }
26+
27+ $ db = App::resolve (Database::class);
28+ // check if the account already exists.
29+ $ user = $ db ->query ('SELECT * FROM users WHERE email = :email ' , [
30+ 'email ' => $ email
31+ ])->find ();
32+
33+ // dd($user);
34+ if ($ user ) {
35+ // then someone with their email already exists and has an account.
36+ // If yes, redirect to the login page.
37+ header ('Location: / ' );
38+ exit (); // or die
39+ } else {
40+ // If no, save one to database, and then log the user in, and redirect.
41+ $ db ->query ('INSERT INTO users (email, password, name) VALUES (:email, :password, :name) ' , [
42+ ':email ' => $ email ,
43+ ':password ' => $ password ,
44+ ':name ' => 'test '
45+ ]);
46+
47+ // mark that the user is logged in.
48+ // you can also use something like this to store the user id in the session.
49+ // $_SESSION['logged_id'];
50+ // but we are gonna take this route
51+ // or may be you add multiple values like below helper
52+ // $_SESSION['logged_in'] = true;
53+ $ _SESSION ['user ' ] = [
54+ 'email ' => $ email ,
55+ ];
56+
57+ header ('Location: / ' );
58+ exit (); // or die
59+ }
0 commit comments