Login form: add remember username feature

This commit is contained in:
Benjamin Renard 2023-04-22 18:56:13 +02:00
parent 8d0907b1ba
commit 43a467b15a
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
7 changed files with 63 additions and 17 deletions

View file

@ -172,6 +172,12 @@ auth:
http: "HTTP" http: "HTTP"
cas: "SSO" cas: "SSO"
# Remember username
# Enable the feature (default: true)
# remember_username: true
# Cookie name (default: remember_username)
# remember_username_cookie_name: "remember_username"
# #
# HTTP Authentication Configuration # HTTP Authentication Configuration
# #

View file

@ -1,7 +1,7 @@
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"POT-Creation-Date: 2023-03-22 19:13+0100\n" "POT-Creation-Date: 2023-04-22 18:48+0200\n"
"PO-Revision-Date: \n" "PO-Revision-Date: \n"
"Last-Translator: Benjamin Renard <brenard@easter-eggs.com>\n" "Last-Translator: Benjamin Renard <brenard@easter-eggs.com>\n"
"Language-Team: \n" "Language-Team: \n"
@ -28,11 +28,11 @@ msgstr "Inconnu"
msgid "Unable to connect to the database." msgid "Unable to connect to the database."
msgstr "Impossible de se connecter à la base de données." msgstr "Impossible de se connecter à la base de données."
#: Auth/Form.php:41 #: Auth/Form.php:51
msgid "Invalid username or password." msgid "Invalid username or password."
msgstr "Nom d'utilisateur ou mot de passe invalide." msgstr "Nom d'utilisateur ou mot de passe invalide."
#: Auth/Form.php:74 #: Auth/Form.php:92
msgid "Sign in" msgid "Sign in"
msgstr "Connexion" msgstr "Connexion"
@ -581,15 +581,19 @@ msgstr "Merci de patienter pendant le traitement de votre requête."
msgid "Are you sure?" msgid "Are you sure?"
msgstr "Êtes-vous sure ?" msgstr "Êtes-vous sure ?"
#: templates/login.tpl:19 templates/login.tpl:20 #: templates/login.tpl:20 templates/login.tpl:21
msgid "Username" msgid "Username"
msgstr "Nom d'utilisateur" msgstr "Nom d'utilisateur"
#: templates/login.tpl:24 templates/login.tpl:25 #: templates/login.tpl:26 templates/login.tpl:27
msgid "Password" msgid "Password"
msgstr "Mot de passe" msgstr "Mot de passe"
#: templates/login.tpl:29 #: templates/login.tpl:33
msgid "Remember username"
msgstr "Mémoriser nom d'utilisateur"
#: templates/login.tpl:38
msgid "Submit" msgid "Submit"
msgstr "Envoyer" msgstr "Envoyer"

View file

@ -1,7 +1,7 @@
msgid "" msgid ""
msgstr "" msgstr ""
"POT-Creation-Date: 2023-03-22 19:13+0100\n" "POT-Creation-Date: 2023-04-22 18:48+0200\n"
"PO-Revision-Date: 2023-03-22 19:13+0100\n" "PO-Revision-Date: 2023-04-22 18:48+0200\n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
@ -22,11 +22,11 @@ msgstr ""
msgid "Unable to connect to the database." msgid "Unable to connect to the database."
msgstr "" msgstr ""
#: Auth/Form.php:41 #: Auth/Form.php:51
msgid "Invalid username or password." msgid "Invalid username or password."
msgstr "" msgstr ""
#: Auth/Form.php:74 #: Auth/Form.php:92
msgid "Sign in" msgid "Sign in"
msgstr "" msgstr ""
@ -504,15 +504,19 @@ msgstr ""
msgid "Are you sure?" msgid "Are you sure?"
msgstr "" msgstr ""
#: templates/login.tpl:19 templates/login.tpl:20 #: templates/login.tpl:20 templates/login.tpl:21
msgid "Username" msgid "Username"
msgstr "" msgstr ""
#: templates/login.tpl:24 templates/login.tpl:25 #: templates/login.tpl:26 templates/login.tpl:27
msgid "Password" msgid "Password"
msgstr "" msgstr ""
#: templates/login.tpl:29 #: templates/login.tpl:33
msgid "Remember username"
msgstr ""
#: templates/login.tpl:38
msgid "Submit" msgid "Submit"
msgstr "" msgstr ""

View file

@ -172,6 +172,12 @@ auth:
http: "HTTP" http: "HTTP"
cas: "SSO" cas: "SSO"
# Remember username
# Enable the feature (default: true)
# remember_username: true
# Cookie name (default: remember_username)
# remember_username_cookie_name: "remember_username"
# #
# HTTP Authentication Configuration # HTTP Authentication Configuration
# #

View file

@ -22,6 +22,8 @@ class Form extends Method {
array( array(
'display_other_methods' => array(), 'display_other_methods' => array(),
'include_navbar' => true, 'include_navbar' => true,
'remember_username' => true,
'remember_username_cookie_name' => 'remember_username',
) )
); );
Url :: add_url_handler('#^login$#', array('EesyPHP\\Auth\\Form', 'handle_login'), null, false); Url :: add_url_handler('#^login$#', array('EesyPHP\\Auth\\Form', 'handle_login'), null, false);
@ -38,7 +40,15 @@ class Form extends Method {
$user = null; $user = null;
if (isset($_REQUEST['username']) && isset($_REQUEST['password'])) { if (isset($_REQUEST['username']) && isset($_REQUEST['password'])) {
$user = Auth :: authenticate($_REQUEST['username'], $_REQUEST['password']); $user = Auth :: authenticate($_REQUEST['username'], $_REQUEST['password']);
if (!$user) Tpl::add_error(_('Invalid username or password.')); if ($user) {
setcookie(
App::get('auth.login_form.remember_username_cookie_name'),
App::get('auth.login_form.remember_username') && isset($_REQUEST['remember-username'])?
$user->username:null
);
}
else
Tpl::add_error(_('Invalid username or password.'));
} }
if ($force && !$user) { if ($force && !$user) {
if (Url :: get_current_url() != 'login') if (Url :: get_current_url() != 'login')
@ -67,8 +77,15 @@ class Form extends Method {
} }
if ($user) if ($user)
Url :: redirect(isset($_REQUEST['next'])?urldecode($_REQUEST['next']):null); Url :: redirect(isset($_REQUEST['next'])?urldecode($_REQUEST['next']):null);
else
Tpl :: assign('next', (isset($_REQUEST['next'])?urldecode($_REQUEST['next']):'')); $remember_username = App::get('auth.login_form.remember_username', null, 'bool');
Tpl :: assign('remember_username', $remember_username);
if ($remember_username) {
$cookie_name = App::get('auth.login_form.remember_username_cookie_name');
Tpl :: assign('username', isset($_COOKIE[$cookie_name])?$_COOKIE[$cookie_name]:null);
}
Tpl :: assign('next', isset($_REQUEST['next'])?urldecode($_REQUEST['next']):'');
Tpl :: assign('display_other_methods', $display_other_methods); Tpl :: assign('display_other_methods', $display_other_methods);
Tpl :: assign('include_navbar', App::get('auth.login_form.include_navbar', null, 'bool')); Tpl :: assign('include_navbar', App::get('auth.login_form.include_navbar', null, 'bool'));
Tpl :: display('login.tpl', I18n::_('Sign in')); Tpl :: display('login.tpl', I18n::_('Sign in'));

View file

@ -16,7 +16,7 @@
<div class="form-floating"> <div class="form-floating">
<input type="text" class="form-control" id="input_username" name="username" <input type="text" class="form-control" id="input_username" name="username"
placeholder="{t domain=$CORE_TEXT_DOMAIN}Username{/t}"/> placeholder="{t domain=$CORE_TEXT_DOMAIN}Username{/t}" value="{if $remember_username && $username}{$username|escape:"quotes"}{/if}"/>
<label for="input_username">{t domain=$CORE_TEXT_DOMAIN}Username{/t}</label> <label for="input_username">{t domain=$CORE_TEXT_DOMAIN}Username{/t}</label>
</div> </div>
<div class="form-floating"> <div class="form-floating">
@ -25,6 +25,15 @@
<label for="input_password">{t domain=$CORE_TEXT_DOMAIN}Password{/t}</label> <label for="input_password">{t domain=$CORE_TEXT_DOMAIN}Password{/t}</label>
</div> </div>
{if $remember_username}
<div class="checkbox mb-3">
<label>
<input type="checkbox" name="remember-username" value="remember" {if $username}checked{/if}>
{t domain=$CORE_TEXT_DOMAIN}Remember username{/t}
</label>
</div>
{/if}
<button class="w-100 btn btn-lg btn-primary" type="submit"> <button class="w-100 btn btn-lg btn-primary" type="submit">
{t domain=$CORE_TEXT_DOMAIN}Submit{/t} {t domain=$CORE_TEXT_DOMAIN}Submit{/t}
</button> </button>