Skip to content

Commit 9a7be2a

Browse files
committed
register a new user 7:25:00
1 parent 37b15b5 commit 9a7be2a

File tree

9 files changed

+140
-6
lines changed

9 files changed

+140
-6
lines changed

controllers/about.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<?php
22

3+
// you cant use $_SESSION here because session_start() is not called
4+
// you need to start the session then you can use session variables
5+
// keep that in mind session variables are not available all the times
6+
// it gets automatically destroyed when the session ends or you close the tab or browser
7+
// 6:59:18
8+
// session data is temporary
9+
// dd($_SESSION['name']);
10+
311
view("about.view.php", [
412
'heading' => 'About Us',
513
]);

controllers/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
$_SESSION['name'] = 'John Doe';
4+
35
view("index.view.php", [
46
'heading' => 'Home',
57
]);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
4+
view('registration/create.view.php', [
5+
6+
]);

controllers/registration/store.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

public/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
session_start();
4+
35
const BASE_PATH = __DIR__.'/../';
46

57
require BASE_PATH.'Core/functions.php';

routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
$router->get('/notes/create', 'controllers/notes/create.php');
1515
$router->post('/notes', 'controllers/notes/store.php');
1616

17+
$router->get('/register', 'controllers/registration/create.php');
18+
$router->post('/register', 'controllers/registration/store.php');

views/partials/banner.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<header class="bg-white shadow">
22
<div class="mx-auto max-w-7xl py-6 px-4 sm:px-6 lg:px-8">
3-
<h1 class="text-3xl font-bold tracking-tight text-gray-900"><?= $heading ?></h1>
3+
<h1 class="text-3xl font-bold tracking-tight text-gray-900"><?= $heading ?>
4+
Hello, <?= $_SESSION['name'] ?? 'Guest' ?>
5+
</h1>
46
</div>
57
</header>

views/partials/nav.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="flex items-center">
55
<div class="flex-shrink-0">
66
<!-- <img class="h-8 w-8" src="https://tailwindui.com/img/logos/mark.svg?color=indigo&shade=500" alt="Your Company"> -->
7-
<p>image</p>
7+
<p>image</p>
88
</div>
99
<div class="hidden md:block">
1010
<div class="ml-10 flex items-baseline space-x-4">
@@ -29,10 +29,14 @@
2929
<!-- Profile dropdown -->
3030
<div class="relative ml-3">
3131
<div>
32-
<button type="button" class="flex max-w-xs items-center rounded-full bg-gray-800 text-sm focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800" id="user-menu-button" aria-expanded="false" aria-haspopup="true">
33-
<span class="sr-only">Open user menu</span>
34-
<img class="h-8 w-8 rounded-full" src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80" alt="">
35-
</button>
32+
<?php if ($_SESSION['user'] ?? false) : ?>
33+
<button type="button" class="flex max-w-xs items-center rounded-full bg-gray-800 text-sm focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-gray-800" id="user-menu-button" aria-expanded="false" aria-haspopup="true">
34+
<span class="sr-only">Open user menu</span>
35+
<img class="h-8 w-8 rounded-full" src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80" alt="">
36+
</button>
37+
<?php else : ?>
38+
<a href="/register" class="text-white">Register</a>
39+
<?php endif; ?>
3640
</div>
3741
</div>
3842
</div>

views/registration/create.view.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php require base_path('views/partials/head.php') ?>
2+
<?php require base_path('views/partials/nav.php') ?>
3+
4+
<main>
5+
<div class="flex min-h-full items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
6+
<div class="w-full max-w-md space-y-8">
7+
<div>
8+
<h2 class="mt-6 text-center text-3xl font-bold tracking-tight text-gray-900">Register for a new
9+
account</h2>
10+
</div>
11+
12+
<form class="mt-8 space-y-6" action="/register" method="POST">
13+
<div class="-space-y-px rounded-md shadow-sm">
14+
<div>
15+
<label for="email" class="sr-only">Email address</label>
16+
<input id="email" name="email" type="email" autocomplete="email" required
17+
class="relative block w-full appearance-none rounded-none rounded-t-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm"
18+
placeholder="Email address">
19+
</div>
20+
21+
<div>
22+
<label for="password" class="sr-only">Password</label>
23+
<input id="password" name="password" type="password" autocomplete="current-password" required
24+
class="relative block w-full appearance-none rounded-none rounded-b-md border border-gray-300 px-3 py-2 text-gray-900 placeholder-gray-500 focus:z-10 focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm"
25+
placeholder="Password">
26+
</div>
27+
</div>
28+
29+
<div>
30+
<button type="submit"
31+
class="group relative flex w-full justify-center rounded-md border border-transparent bg-indigo-600 py-2 px-4 text-sm font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">
32+
Register
33+
</button>
34+
</div>
35+
<ul>
36+
<?php if (isset($errors['email'])) : ?>
37+
<li class="text-red-500 text-xs mt-2"><?= $errors['email'] ?></li>
38+
<?php endif; ?>
39+
40+
<?php if (isset($errors['password'])) : ?>
41+
<li class="text-red-500 text-xs mt-2"><?= $errors['password'] ?></li>
42+
<?php endif; ?>
43+
</ul>
44+
</form>
45+
</div>
46+
</div>
47+
</main>
48+
49+
<?php require base_path('views/partials/footer.php') ?>

0 commit comments

Comments
 (0)