Sign in with email address?

Hello,
I would like to know if it is possible to connect with the email address?
(This tutorial: https://getkirby.com/docs/cookbook/authentication)
I try to replace “username” with “email” but it does not work.

Maybe I missed something?

Thank.

I wanted to check for myself because I also did this tutorial a while ago, and actually it seems that with the password, it does not work. Also it seems that the name or the first name alone does not work either.

Maybe a possible issue due to the core ?

$user = $site->user(get('email')) and $user->login(get('password')) => doesn’t work

You need to find the user by email address like this:

$user = $site->users()->findBy('email', get('email'));

Apart from that it’s the same. :slight_smile:

1 Like

I had not thought of “findBy”…
Thank you very much!

Solution:

controllers/login.php

<?php 

return function($site, $pages, $page) {

  // don't show the login screen to already logged in users
  if($site->user()) go('/');

  // handle the form submission
  if(r::is('post') and get('login')) {

    // fetch the user by email and run the 
    // login method with the password
    if($user = $site->users()->findBy('email', get('email')) and $user->login(get('password'))) {
      // redirect to the homepage 
      // if the login was successful
      go('account');
    } else {
      // make sure the alert is being 
      // displayed in the template
      $error = true;
    }

  } else {
    // nothing has been submitted
    // nothing has gone wrong
    $error = false;  
  }

  return compact('error');

};

And,
template/login.php

<?php snippet('header') ?>

<main class="main" role="main">

	<div class="text">
		<h1 class="title"><?php echo $page->subtitle()->or($page->title()) ?></h1>
		<?php echo $page->text()->kirbytext() ?>
	</diiv>
	
	<form method="post">
		<?php if($error): ?>
		<div class="alert error">
			<p><?php echo $page->alert()->html() ?></p>
		</div>
		<?php endif ?>

		<div class="field">
			<label for="email">Email</label>
			<input type="text" id="email" name="email">
		</div>
		<div class="field">
			<label for="password">Password </label>
			<input type="password" id="password" name="password">
		</div>
		<div class="field">
			<input class="btn" type="submit" name="login" value="Login">
		</div>
	</form>

</main>

<?php snippet('footer') ?>
1 Like

If I’m correct, this is for a custom built login page? Is it possible to change the regular panel login page to use email address instead of username?

People forget their passwords, but also their usernames… Email addresses is much better for login ID.

Nope, not at the moment. But you can build a custom login page and then redirect the users to the Panel.