#1019 Add email field to web integration sample

This commit is contained in:
ljacqu 2017-01-22 11:17:31 +01:00
parent cc7deb64c8
commit aa64f96466
2 changed files with 19 additions and 7 deletions

View File

@ -54,16 +54,18 @@ abstract class AuthMeController {
*
* @param string $username the username to register
* @param string $password the password to associate to the user
* @param string $email the email (may be empty)
* @return bool whether or not the registration was successful
*/
function register($username, $password) {
function register($username, $password, $email) {
$email = $email ? $email : 'your@email.com';
$mysqli = $this->getAuthmeMySqli();
if ($mysqli !== null) {
$hash = $this->hash($password);
$stmt = $mysqli->prepare('INSERT INTO ' . self::AUTHME_TABLE . ' (username, realname, password, ip) '
. 'VALUES (?, ?, ?, ?)');
$stmt = $mysqli->prepare('INSERT INTO ' . self::AUTHME_TABLE . ' (username, realname, password, email, ip) '
. 'VALUES (?, ?, ?, ?, ?)');
$username_low = strtolower($username);
$stmt->bind_param('ssss', $username, $username_low, $hash, $_SERVER['REMOTE_ADDR']);
$stmt->bind_param('sssss', $username, $username_low, $hash, $email, $_SERVER['REMOTE_ADDR']);
return $stmt->execute();
}
return false;

View File

@ -22,13 +22,14 @@ $authme_controller = new Sha256();
$action = get_from_post_or_empty('action');
$user = get_from_post_or_empty('username');
$pass = get_from_post_or_empty('password');
$email = get_from_post_or_empty('email');
$was_successful = false;
if ($action && $user && $pass) {
if ($action === 'Log in') {
$was_successful = process_login($user, $pass, $authme_controller);
} else if ($action === 'Register') {
$was_successful = process_register($user, $pass, $authme_controller);
$was_successful = process_register($user, $pass, $email, $authme_controller);
}
}
@ -39,6 +40,7 @@ into the following form to test it.
<form method="post">
<table>
<tr><td>Name</td><td><input type="text" value="' . htmlspecialchars($user) . '" name="username" /></td></tr>
<tr><td>Email</td><td><input type="text" value="' . htmlspecialchars($email) . '" name="email" /></td></tr>
<tr><td>Pass</td><td><input type="password" value="' . htmlspecialchars($pass) . '" name="password" /></td></tr>
<tr>
<td><input type="submit" name="action" value="Log in" /></td>
@ -69,12 +71,14 @@ function process_login($user, $pass, AuthMeController $controller) {
}
// Register logic
function process_register($user, $pass, AuthMeController $controller) {
function process_register($user, $pass, $email, AuthMeController $controller) {
if ($controller->isUserRegistered($user)) {
echo '<h1>Error</h1> This user already exists.';
} else if (!is_email_valid($email)) {
echo '<h1>Error</h1> The supplied email is invalid.';
} else {
// Note that we don't validate the password or username at all in this demo...
$register_success = $controller->register($user, $pass);
$register_success = $controller->register($user, $pass, $email);
if ($register_success) {
printf('<h1>Welcome, %s!</h1>Thanks for registering', htmlspecialchars($user));
echo '<br /><a href="index.php">Back to form</a>';
@ -86,6 +90,12 @@ function process_register($user, $pass, AuthMeController $controller) {
return false;
}
function is_email_valid($email) {
return trim($email) === ''
? true // accept no email
: filter_var($email, FILTER_VALIDATE_EMAIL);
}
?>
</body>