Hi,
I’ve gone through the recipes to create a registration and login page for users to sign up. In the future, I want to show some content only to the ones who signed up.
But when I test my registration page, I don’t get a verification code via email. I will be redirected to the panel page where I have to type in the code but I’m getting no email.
What could be the problem here?
Below is my code I got by following the Kirby recipe.
Before I created the sign up function, I followed the basic recipe for creating a login page and created all files like explained.
The only thing I changed on my own was not to name the user profile “client”, but “subscriber”. I changed this also in the registration.php controller “…‘role’ => ‘subscriber’,…”
(In case it’s helpful: When I try to register and not get a code via email, I can see in the panel however that the account was already created.)
My code:
config.php
<?php
return [
'languages' => true,
'debug' => false,
'email' => [
// see https://getkirby.com/docs/guide/emails#transport-configuration
'transport' => [
'type' => 'smtp',
'host' => 'smtp.easyname.com',
'port' => 465,
'security' => true
]
],
// see https://getkirby.com/docs/reference/system/options/auth#login-methods
'auth' => [
'methods' => ['password', 'code']
],
'routes' => [
[
'pattern' => 'logout',
'action' => function() {
if ($user = kirby()->user()) {
$user->logout();
}
go('login');
}
]
]
];
/controllers/registration.php
<?php
use Kirby\Exception\PermissionException;
return function ($kirby) {
// send already logged-in user somewhere else
if ($kirby->user()) {
go('home');
}
// create empty error list
$errors = [];
// the form was sent
if (get('register') && $kirby->request()->is('POST')) {
// validate CSRF token
if (csrf(get('csrf')) === true) {
// get form data
$data = [
'email' => get('email'),
'name' => get('name'),
];
// validation rules
$rules = [
'email' => ['required', 'email'],
'name' => ['required', 'minLength' => 3],
];
// error messages
$messages = [
'email' => 'Please enter a valid email address',
'name' => 'Your name must have at least 3 characters',
];
// check if data is valid
if ($invalid = invalid($data, $rules, $messages)) {
$errors = $invalid;
// the data is fine, let's create a user
} else {
// authenticate
$kirby->impersonate('kirby');
try {
// create new user
$user = $kirby->users()->create([
'email' => $data['email'],
'role' => 'subscriber',
'language' => 'en',
'name' => $data['name'],
]);
if (isset($user) === true) {
// create the authentication challenge
try {
$status = $kirby->auth()->createChallenge($user->email(), false, 'login');
go('panel/login');
} catch (PermissionException $e) {
$errors[] = $e->getMessage();
}
}
} catch (Exception $e) {
$errors[] = $e->getMessage();
}
}
} else {
$errors[] = 'Invalid CSRF token.';
}
}
return [
'errors' => $errors
];
};
/controllers/login.php
<?php
return function ($kirby) {
// don't show the login screen to already logged in users
if ($kirby->user()) {
go('/');
}
$error = false;
// handle the form submission
if ($kirby->request()->is('POST') && get('login')) {
// try to log the user in with the provided credentials
try {
$kirby->auth()->login(get('email'), get('password'));
// redirect to the homepage if the login was successful
go('/');
} catch (Exception $e) {
$error = true;
}
}
return [
'error' => $error
];
};
templates/registration.php
<?php snippet('header') ?>
<?php snippet('nav') ?>
<div class="intro" style="margin-top: 400px;">
<h1><?= $page->title() ?></h1>
</div>
<?php
// if the form has errors, show a list of messages
if (count($errors) > 0) : ?>
<ul class="alert">
<?php foreach ($errors as $message) : ?>
<li><?= kirbytext($message) ?></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<form method="post" action="<?= $page->url() ?>">
<input type="hidden" name="csrf" value="<?= csrf() ?>">
<div>
<label for="name">Name</label>
<input required type="text" id="name" name="name" value="<?= esc($data['name'] ?? '', 'attr') ?>">
</div>
<div>
<label for="email">Email</label>
<input required type="email" id="email" name="email" value="<?= esc($data['email'] ?? '', 'attr') ?>">
</div>
<div>
<input type="submit" name="register" value="Register">
</div>
</form>
<?php snippet('footer') ?>
/users/subscriber.yml
title: Subscriber
permissions:
access:
panel: false