mirror of
https://github.com/delight-im/PHP-Auth.git
synced 2025-08-06 08:07:27 +02:00
Compare commits
54 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
8acd3a9779 | ||
|
374f27176b | ||
|
3cb2284870 | ||
|
690485ba6d | ||
|
495a87d499 | ||
|
784030139b | ||
|
fb6f3d31b8 | ||
|
370ecc4933 | ||
|
da2d282648 | ||
|
4aaf85e3cf | ||
|
f2561a1932 | ||
|
8cc54473e3 | ||
|
f26f2209cd | ||
|
188086f2e4 | ||
|
c6213a6081 | ||
|
c55250c572 | ||
|
dac2850aba | ||
|
4268e3fcd5 | ||
|
d579179494 | ||
|
bd02e08f83 | ||
|
d4fe11b844 | ||
|
09fabd4c91 | ||
|
4dcf491ad9 | ||
|
4f5ff151ef | ||
|
f5027c09e9 | ||
|
6db82d1f65 | ||
|
f944067aff | ||
|
a640e8a5ad | ||
|
2aee8a662e | ||
|
36ef710480 | ||
|
9187840767 | ||
|
6bfa298836 | ||
|
6be456a27a | ||
|
78a16d8f50 | ||
|
e669f6f017 | ||
|
5aafd0b009 | ||
|
d53a484c2e | ||
|
07732dcaa9 | ||
|
f486ab6763 | ||
|
5e331924f6 | ||
|
ac95be3714 | ||
|
e6c8ae056c | ||
|
5bac29065d | ||
|
36b590eb81 | ||
|
5c6a71d921 | ||
|
d94243f19d | ||
|
2a2d93f534 | ||
|
989c7940e5 | ||
|
51a5735295 | ||
|
e5e465782b | ||
|
83caa3e785 | ||
|
f2a1aedf7a | ||
|
5c87e877db | ||
|
70842b4320 |
@@ -38,3 +38,7 @@
|
||||
## From `v2.x.x` to `v3.x.x`
|
||||
|
||||
* The license has been changed from the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0) to the [MIT License](https://opensource.org/licenses/MIT).
|
||||
|
||||
## From `v3.x.x` to `v4.x.x`
|
||||
|
||||
* PHP 5.6.0 or higher is now required.
|
||||
|
161
README.md
161
README.md
@@ -16,35 +16,57 @@ Completely framework-agnostic and database-agnostic.
|
||||
|
||||
## Requirements
|
||||
|
||||
* PHP 5.5.0+
|
||||
* OpenSSL extension
|
||||
* PHP 5.6.0+
|
||||
* OpenSSL extension (`openssl`)
|
||||
* MySQL 5.5.3+ **or** MariaDB 5.5.23+
|
||||
|
||||
## Installation
|
||||
|
||||
* Set up the PHP library
|
||||
* Install via [Composer](https://getcomposer.org/) (recommended)
|
||||
1. Include the library via [Composer](https://getcomposer.org/):
|
||||
|
||||
`$ composer require delight-im/auth`
|
||||
```
|
||||
$ composer require delight-im/auth
|
||||
```
|
||||
|
||||
Include the Composer autoloader:
|
||||
1. Include the Composer autoloader:
|
||||
|
||||
`require __DIR__.'/vendor/autoload.php';`
|
||||
```php
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
```
|
||||
|
||||
* or
|
||||
* Install manually
|
||||
* Copy the contents of the [`src`](src) directory to a subfolder of your project
|
||||
* Include the files in your code via `require` or `require_once`
|
||||
* Set up a database and create the required tables
|
||||
* [MySQL](Database/MySQL.sql)
|
||||
1. Set up a database and create the required tables:
|
||||
|
||||
* [MySQL](Database/MySQL.sql)
|
||||
|
||||
## Usage
|
||||
|
||||
### Create a new instance
|
||||
* [Creating a new instance](#creating-a-new-instance)
|
||||
* [Registration (sign up)](#registration-sign-up)
|
||||
* [Login (sign in)](#login-sign-in)
|
||||
* [Email verification](#email-verification)
|
||||
* [Keeping the user logged in](#keeping-the-user-logged-in)
|
||||
* [Password reset ("forgot password")](#password-reset-forgot-password)
|
||||
* [Changing the current user's password](#changing-the-current-users-password)
|
||||
* [Logout](#logout)
|
||||
* [Accessing user information](#accessing-user-information)
|
||||
* [Login state](#login-state)
|
||||
* [User ID](#user-id)
|
||||
* [Email address](#email-address)
|
||||
* [Display name](#display-name)
|
||||
* [Checking whether the user was "remembered"](#checking-whether-the-user-was-remembered)
|
||||
* [IP address](#ip-address)
|
||||
* [Additional user information](#additional-user-information)
|
||||
* [Utilities](#utilities)
|
||||
* [Creating a random string](#creating-a-random-string)
|
||||
* [Creating a UUID v4 as per RFC 4122](#creating-a-uuid-v4-as-per-rfc-4122)
|
||||
* [Reading and writing session data](#reading-and-writing-session-data)
|
||||
|
||||
### Creating a new instance
|
||||
|
||||
```php
|
||||
// $db = new PDO('mysql:dbname=database;host=localhost;charset=utf8mb4', 'username', 'password');
|
||||
// $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
// $db = new PDO('mysql:dbname=my-database;host=localhost;charset=utf8mb4', 'my-username', 'my-password');
|
||||
// or
|
||||
// $db = new \Delight\Db\PdoDsn('mysql:dbname=my-database;host=localhost;charset=utf8mb4', 'my-username', 'my-password');
|
||||
|
||||
$auth = new \Delight\Auth\Auth($db);
|
||||
```
|
||||
@@ -57,7 +79,7 @@ Only in the very rare case that you need access to your cookies from JavaScript,
|
||||
|
||||
If your web server is behind a proxy server and `$_SERVER['REMOTE_ADDR']` only contains the proxy's IP address, you must pass the user's real IP address to the constructor in the fourth argument. The default is `null`.
|
||||
|
||||
### Sign up a new user (register)
|
||||
### Registration (sign up)
|
||||
|
||||
```php
|
||||
try {
|
||||
@@ -83,19 +105,21 @@ catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
|
||||
The username in the third parameter is optional. You can pass `null` here if you don't want to manage usernames.
|
||||
|
||||
If you want to enforce unique usernames, on the other hand, simply call `registerWithUniqueUsername` instead of `register`, and be prepared to catch the `DuplicateUsernameException`, if necessary.
|
||||
|
||||
For email verification, you should build an URL with the selector and token and send it to the user, e.g.:
|
||||
|
||||
```php
|
||||
$url = 'https://www.example.com/verify_email?selector='.urlencode($selector).'&token='.urlencode($token);
|
||||
```
|
||||
|
||||
If you don't want to perform email verification, just omit the last parameter to `register(...)`. The new user will be active immediately, then.
|
||||
If you don't want to perform email verification, just omit the last parameter to `Auth#register`. The new user will be active immediately, then.
|
||||
|
||||
### Sign in an existing user (login)
|
||||
### Login (sign in)
|
||||
|
||||
```php
|
||||
try {
|
||||
$auth->login($_POST['email'], $_POST['password'], ($_POST['remember'] == 1));
|
||||
$auth->login($_POST['email'], $_POST['password']);
|
||||
|
||||
// user is logged in
|
||||
}
|
||||
@@ -113,13 +137,9 @@ catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
}
|
||||
```
|
||||
|
||||
The third parameter controls whether the login is persistent with a long-lived cookie. With such a persistent login, users may stay authenticated for a long time, even when the browser session has already been closed and the session cookies have expired. Typically, you'll want to keep the user logged in for weeks or months with this feature, which is known as "remember me" or "keep me logged in". Many users will find this more convenient, but it may be less secure if they leave their devices unattended.
|
||||
If you want to sign in with usernames on the other hand, either in addition to the login via email address or as a replacement, that's possible as well. Simply call the method `loginWithUsername` instead of method `login`. Then, instead of catching `InvalidEmailException`, make sure to catch both `UnknownUsernameException` and `AmbiguousUsernameException`. You may also want to read the notes about the uniqueness of usernames in the section that explains how to [sign up new users](#registration-sign-up).
|
||||
|
||||
*Without* the persistent login, which is the *default* behavior, a user will only stay logged in until they close their browser, or as long as configured via `session.cookie_lifetime` and `session.gc_maxlifetime` in PHP.
|
||||
|
||||
Set the third parameter to `false` to disable the feature. Otherwise, ask the user if they want to enable "remember me". This is usually done with a checkbox in your user interface. Use the input from that checkbox to decide between `false` and `true` here. This is optional and the default is `false`.
|
||||
|
||||
### Perform email verification
|
||||
### Email verification
|
||||
|
||||
Extract the selector and token from the URL that the user clicked on in the verification email.
|
||||
|
||||
@@ -140,7 +160,32 @@ catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
}
|
||||
```
|
||||
|
||||
### Reset a password ("forgot password")
|
||||
### Keeping the user logged in
|
||||
|
||||
The third parameter to the `Auth#login` method controls whether the login is persistent with a long-lived cookie. With such a persistent login, users may stay authenticated for a long time, even when the browser session has already been closed and the session cookies have expired. Typically, you'll want to keep the user logged in for weeks or months with this feature, which is known as "remember me" or "keep me logged in". Many users will find this more convenient, but it may be less secure if they leave their devices unattended.
|
||||
|
||||
```php
|
||||
if ($_POST['remember'] == 1) {
|
||||
// keep logged in for one year
|
||||
$rememberDuration = (int) (60 * 60 * 24 * 365.25);
|
||||
}
|
||||
else {
|
||||
// do not keep logged in after session ends
|
||||
$rememberDuration = null;
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
$auth->login($_POST['email'], $_POST['password'], $rememberDuration);
|
||||
|
||||
// ...
|
||||
```
|
||||
|
||||
*Without* the persistent login, which is the *default* behavior, a user will only stay logged in until they close their browser, or as long as configured via `session.cookie_lifetime` and `session.gc_maxlifetime` in PHP.
|
||||
|
||||
Omit the third parameter or set it to `null` to disable the feature. Otherwise, you may ask the user whether they want to enable "remember me". This is usually done with a checkbox in your user interface. Use the input from that checkbox to decide between `null` and a pre-defined duration in seconds here, e.g. `60 * 60 * 24 * 365.25` for one year.
|
||||
|
||||
### Password reset ("forgot password")
|
||||
|
||||
```php
|
||||
try {
|
||||
@@ -153,6 +198,9 @@ try {
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
// invalid email address
|
||||
}
|
||||
catch (\Delight\Auth\EmailNotVerifiedException $e) {
|
||||
// email not verified
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
}
|
||||
@@ -199,7 +247,7 @@ catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
}
|
||||
```
|
||||
|
||||
### Change the current user's password
|
||||
### Changing the current user's password
|
||||
|
||||
If a user is currently logged in, they may change their password.
|
||||
|
||||
@@ -225,7 +273,9 @@ $auth->logout();
|
||||
// user has been signed out
|
||||
```
|
||||
|
||||
### Check if the user is signed in
|
||||
### Accessing user information
|
||||
|
||||
#### Login state
|
||||
|
||||
```php
|
||||
if ($auth->isLoggedIn()) {
|
||||
@@ -238,7 +288,7 @@ else {
|
||||
|
||||
A shorthand/alias for this method is `$auth->check()`.
|
||||
|
||||
### Get the user's ID
|
||||
#### User ID
|
||||
|
||||
```php
|
||||
$id = $auth->getUserId();
|
||||
@@ -248,7 +298,7 @@ If the user is not currently signed in, this returns `null`.
|
||||
|
||||
A shorthand/alias for this method is `$auth->id()`.
|
||||
|
||||
### Get the user's email address
|
||||
#### Email address
|
||||
|
||||
```php
|
||||
$email = $auth->getEmail();
|
||||
@@ -256,7 +306,7 @@ $email = $auth->getEmail();
|
||||
|
||||
If the user is not currently signed in, this returns `null`.
|
||||
|
||||
### Get the user's display name
|
||||
#### Display name
|
||||
|
||||
```php
|
||||
$email = $auth->getUsername();
|
||||
@@ -266,7 +316,7 @@ Remember that usernames are optional and there is only a username if you supplie
|
||||
|
||||
If the user is not currently signed in, this returns `null`.
|
||||
|
||||
### Check if the user was "remembered"
|
||||
#### Checking whether the user was "remembered"
|
||||
|
||||
```php
|
||||
if ($auth->isRemembered()) {
|
||||
@@ -279,31 +329,56 @@ else {
|
||||
|
||||
If the user is not currently signed in, this returns `null`.
|
||||
|
||||
### Get the user's IP address
|
||||
#### IP address
|
||||
|
||||
```php
|
||||
$ip = $auth->getIpAddress();
|
||||
```
|
||||
|
||||
### Read and write session data
|
||||
#### Additional user information
|
||||
|
||||
For detailed information on how to read and write session data conveniently, please refer to [the documentation of the session library](https://github.com/delight-im/PHP-Cookie), which is included by default.
|
||||
In order to preserve this library's suitability for all purposes as well as its full re-usability, it doesn't come with additional bundled columns for user information. But you don't have to do without additional user information, of course:
|
||||
|
||||
Here's how to use this library with your own tables for custom user information in a maintainable and re-usable way:
|
||||
|
||||
1. Add any number of custom database tables where you store custom user information, e.g. a table named `profiles`.
|
||||
1. Whenever you call the `register` method (which returns the new user's ID), add your own logic afterwards that fills your custom database tables.
|
||||
1. If you need the custom user information only rarely, you may just retrieve it as needed. If you need it more frequently, however, you'd probably want to have it in your session data. The following method is how you can load and access your data in a reliable way:
|
||||
|
||||
```php
|
||||
function getUserInfo(\Delight\Auth\Auth $auth) {
|
||||
if (!$auth->isLoggedIn()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isset($_SESSION['_internal_user_info'])) {
|
||||
// TODO: load your custom user information and assign it to the session variable below
|
||||
// $_SESSION['_internal_user_info'] = ...
|
||||
}
|
||||
|
||||
return $_SESSION['_internal_user_info'];
|
||||
}
|
||||
```
|
||||
|
||||
### Utilities
|
||||
|
||||
#### Create a random string
|
||||
#### Creating a random string
|
||||
|
||||
```php
|
||||
$length = 24;
|
||||
$randomStr = \Delight\Auth\Auth::createRandomString($length);
|
||||
```
|
||||
|
||||
#### Create a UUID v4 as per RFC 4122
|
||||
#### Creating a UUID v4 as per RFC 4122
|
||||
|
||||
```php
|
||||
$uuid = \Delight\Auth\Auth::createUuid();
|
||||
```
|
||||
|
||||
### Reading and writing session data
|
||||
|
||||
For detailed information on how to read and write session data conveniently, please refer to [the documentation of the session library](https://github.com/delight-im/PHP-Cookie#reading-and-writing-session-data), which is included by default.
|
||||
|
||||
## Features
|
||||
|
||||
* registration
|
||||
@@ -349,12 +424,12 @@ This library throws two types of exceptions to indicate problems:
|
||||
|
||||
## General advice
|
||||
|
||||
* Both serving the authentication pages (e.g. login and registration) and submitting the data entered by the user should only be done over TLS (HTTPS).
|
||||
* You should enforce a minimum length for passwords, e.g. 10 characters, but *no* maximum length. Moreover, you should not restrict the set of allowed characters.
|
||||
* Whenever a user was remembered ("remember me") and did not log in by entering their password, you should require re-authentication for critical features.
|
||||
* Serve *all* pages over HTTPS only, i.e. using SSL/TLS for every single request.
|
||||
* You should enforce a minimum length for passwords, e.g. 10 characters, but *never* any maximum length, at least not anywhere below 100 characters. Moreover, you should *not* restrict the set of allowed characters.
|
||||
* Whenever a user was remembered through the "remember me" feature enabled or disabled during sign in, which means that they did not log in by typing their password, you should require re-authentication for critical features.
|
||||
* Encourage users to use pass*phrases*, i.e. combinations of words or even full sentences, instead of single pass*words*.
|
||||
* Do not prevent users' password managers from working correctly. Thus please use the standard form fields only and do not prevent copy and paste.
|
||||
* Before executing sensitive account operations (e.g. changing a user's email address, deleting a user's account), you should always require re-authentication, i.e. require the user to sign in once more.
|
||||
* Do not prevent users' password managers from working correctly. Thus, use the standard form fields only and do not prevent copy and paste.
|
||||
* Before executing sensitive account operations (e.g. changing a user's email address, deleting a user's account), you should always require re-authentication, i.e. require the user to verify their login credentials once more.
|
||||
* You should not offer an online password reset feature ("forgot password") for high-security applications.
|
||||
* For high-security applications, you should not use email addresses as identifiers. Instead, choose identifiers that are specific to the application and secret, e.g. an internal customer number.
|
||||
|
||||
|
@@ -2,9 +2,10 @@
|
||||
"name": "delight-im/auth",
|
||||
"description": "Authentication for PHP. Simple, lightweight and secure.",
|
||||
"require": {
|
||||
"php": ">=5.5.0",
|
||||
"php": ">=5.6.0",
|
||||
"ext-openssl": "*",
|
||||
"delight-im/cookie": "^2.0"
|
||||
"delight-im/cookie": "^2.1",
|
||||
"delight-im/db": "^1.0"
|
||||
},
|
||||
"type": "library",
|
||||
"keywords": [ "auth", "authentication", "login", "security" ],
|
||||
|
57
composer.lock
generated
57
composer.lock
generated
@@ -4,21 +4,21 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"hash": "22e56875c7a1386807d5cf6ae01f50fa",
|
||||
"content-hash": "b914ccd7ac15e1519d7a04b55dbe725e",
|
||||
"hash": "bd80e3e52b8bd8a4a0c74c7cf9f5bf5e",
|
||||
"content-hash": "3f836c43e0ff2293051f2ccb739d23cf",
|
||||
"packages": [
|
||||
{
|
||||
"name": "delight-im/cookie",
|
||||
"version": "v2.0.0",
|
||||
"version": "v2.1.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/delight-im/PHP-Cookie.git",
|
||||
"reference": "a746f4096885b6715a640a2122b1c21324624f8f"
|
||||
"reference": "3e41e0d44959b59de98722b5b1b1fb83f9f528f3"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/delight-im/PHP-Cookie/zipball/a746f4096885b6715a640a2122b1c21324624f8f",
|
||||
"reference": "a746f4096885b6715a640a2122b1c21324624f8f",
|
||||
"url": "https://api.github.com/repos/delight-im/PHP-Cookie/zipball/3e41e0d44959b59de98722b5b1b1fb83f9f528f3",
|
||||
"reference": "3e41e0d44959b59de98722b5b1b1fb83f9f528f3",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -46,7 +46,48 @@
|
||||
"samesite",
|
||||
"xss"
|
||||
],
|
||||
"time": "2016-07-21 15:20:20"
|
||||
"time": "2016-11-23 20:09:42"
|
||||
},
|
||||
{
|
||||
"name": "delight-im/db",
|
||||
"version": "v1.0.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/delight-im/PHP-DB.git",
|
||||
"reference": "c8d1eba6583007471d55bf7d88eb3c9d87ea849d"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/delight-im/PHP-DB/zipball/c8d1eba6583007471d55bf7d88eb3c9d87ea849d",
|
||||
"reference": "c8d1eba6583007471d55bf7d88eb3c9d87ea849d",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-pdo": "*",
|
||||
"php": ">=5.6.0"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Delight\\Db\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"description": "Safe and convenient SQL database access in a driver-agnostic way",
|
||||
"homepage": "https://github.com/delight-im/PHP-DB",
|
||||
"keywords": [
|
||||
"database",
|
||||
"mysql",
|
||||
"pdo",
|
||||
"pgsql",
|
||||
"postgresql",
|
||||
"sql",
|
||||
"sqlite"
|
||||
],
|
||||
"time": "2016-12-01 12:40:36"
|
||||
},
|
||||
{
|
||||
"name": "delight-im/http",
|
||||
@@ -92,7 +133,7 @@
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=5.5.0",
|
||||
"php": ">=5.6.0",
|
||||
"ext-openssl": "*"
|
||||
},
|
||||
"platform-dev": []
|
||||
|
869
src/Auth.php
869
src/Auth.php
File diff suppressed because it is too large
Load Diff
@@ -12,6 +12,8 @@ class AuthException extends \Exception {}
|
||||
|
||||
class InvalidEmailException extends AuthException {}
|
||||
|
||||
class UnknownUsernameException extends AuthException {}
|
||||
|
||||
class InvalidPasswordException extends AuthException {}
|
||||
|
||||
class EmailNotVerifiedException extends AuthException {}
|
||||
@@ -26,6 +28,10 @@ class TokenExpiredException extends AuthException {}
|
||||
|
||||
class TooManyRequestsException extends AuthException {}
|
||||
|
||||
class DuplicateUsernameException extends AuthException {}
|
||||
|
||||
class AmbiguousUsernameException extends AuthException {}
|
||||
|
||||
class AuthError extends \Exception {}
|
||||
|
||||
class DatabaseError extends AuthError {}
|
||||
@@ -33,3 +39,5 @@ class DatabaseError extends AuthError {}
|
||||
class MissingCallbackError extends AuthError {}
|
||||
|
||||
class HeadersAlreadySentError extends AuthError {}
|
||||
|
||||
class EmailOrUsernameRequiredError extends AuthError {}
|
||||
|
@@ -12,15 +12,14 @@ ini_set('display_errors', 'stdout');
|
||||
|
||||
// enable assertions
|
||||
ini_set('assert.active', 1);
|
||||
ini_set('zend.assertions', 1);
|
||||
@ini_set('zend.assertions', 1);
|
||||
ini_set('assert.exception', 1);
|
||||
|
||||
header('Content-type: text/html; charset=utf-8');
|
||||
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
$db = new PDO('mysql:dbname=php_auth;host=127.0.0.1;charset=utf8mb4', 'root', '');
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$db = new PDO('mysql:dbname=php_auth;host=127.0.0.1;charset=utf8mb4', 'root', 'monkey');
|
||||
|
||||
$auth = new \Delight\Auth\Auth($db);
|
||||
|
||||
@@ -39,14 +38,37 @@ function processRequestData(\Delight\Auth\Auth $auth) {
|
||||
if (isset($_POST)) {
|
||||
if (isset($_POST['action'])) {
|
||||
if ($_POST['action'] === 'login') {
|
||||
if ($_POST['remember'] == 1) {
|
||||
// keep logged in for one year
|
||||
$rememberDuration = (int) (60 * 60 * 24 * 365.25);
|
||||
}
|
||||
else {
|
||||
// do not keep logged in after session ends
|
||||
$rememberDuration = null;
|
||||
}
|
||||
|
||||
try {
|
||||
$auth->login($_POST['email'], $_POST['password'], ($_POST['remember'] == 1));
|
||||
if (isset($_POST['email'])) {
|
||||
$auth->login($_POST['email'], $_POST['password'], $rememberDuration);
|
||||
}
|
||||
elseif (isset($_POST['username'])) {
|
||||
$auth->loginWithUsername($_POST['username'], $_POST['password'], $rememberDuration);
|
||||
}
|
||||
else {
|
||||
return 'either email address or username required';
|
||||
}
|
||||
|
||||
return 'ok';
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
return 'wrong email address';
|
||||
}
|
||||
catch (\Delight\Auth\UnknownUsernameException $e) {
|
||||
return 'unknown username';
|
||||
}
|
||||
catch (\Delight\Auth\AmbiguousUsernameException $e) {
|
||||
return 'ambiguous username';
|
||||
}
|
||||
catch (\Delight\Auth\InvalidPasswordException $e) {
|
||||
return 'wrong password';
|
||||
}
|
||||
@@ -78,7 +100,16 @@ function processRequestData(\Delight\Auth\Auth $auth) {
|
||||
$callback = null;
|
||||
}
|
||||
|
||||
return $auth->register($_POST['email'], $_POST['password'], $_POST['username'], $callback);
|
||||
if (!isset($_POST['require_unique_username'])) {
|
||||
$_POST['require_unique_username'] = '0';
|
||||
}
|
||||
|
||||
if ($_POST['require_unique_username'] == 0) {
|
||||
return $auth->register($_POST['email'], $_POST['password'], $_POST['username'], $callback);
|
||||
}
|
||||
else {
|
||||
return $auth->registerWithUniqueUsername($_POST['email'], $_POST['password'], $_POST['username'], $callback);
|
||||
}
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
return 'invalid email address';
|
||||
@@ -87,7 +118,10 @@ function processRequestData(\Delight\Auth\Auth $auth) {
|
||||
return 'invalid password';
|
||||
}
|
||||
catch (\Delight\Auth\UserAlreadyExistsException $e) {
|
||||
return 'user already exists';
|
||||
return 'email address already exists';
|
||||
}
|
||||
catch (\Delight\Auth\DuplicateUsernameException $e) {
|
||||
return 'username already exists';
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
return 'too many requests';
|
||||
@@ -130,6 +164,9 @@ function processRequestData(\Delight\Auth\Auth $auth) {
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
return 'invalid email address';
|
||||
}
|
||||
catch (\Delight\Auth\EmailNotVerifiedException $e) {
|
||||
return 'email not verified';
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
return 'too many requests';
|
||||
}
|
||||
@@ -249,10 +286,21 @@ function showGuestUserForm() {
|
||||
echo '<input type="text" name="email" placeholder="Email" /> ';
|
||||
echo '<input type="text" name="password" placeholder="Password" /> ';
|
||||
echo '<select name="remember" size="1">';
|
||||
echo '<option value="0">Remember (28 days)? — No</option>';
|
||||
echo '<option value="1">Remember (28 days)? — Yes</option>';
|
||||
echo '<option value="0">Remember (keep logged in)? — No</option>';
|
||||
echo '<option value="1">Remember (keep logged in)? — Yes</option>';
|
||||
echo '</select> ';
|
||||
echo '<button type="submit">Login</button>';
|
||||
echo '<button type="submit">Log in with email address</button>';
|
||||
echo '</form>';
|
||||
|
||||
echo '<form action="" method="post" accept-charset="utf-8">';
|
||||
echo '<input type="hidden" name="action" value="login" />';
|
||||
echo '<input type="text" name="username" placeholder="Username" /> ';
|
||||
echo '<input type="text" name="password" placeholder="Password" /> ';
|
||||
echo '<select name="remember" size="1">';
|
||||
echo '<option value="0">Remember (keep logged in)? — No</option>';
|
||||
echo '<option value="1">Remember (keep logged in)? — Yes</option>';
|
||||
echo '</select> ';
|
||||
echo '<button type="submit">Log in with username</button>';
|
||||
echo '</form>';
|
||||
|
||||
echo '<form action="" method="post" accept-charset="utf-8">';
|
||||
@@ -264,6 +312,10 @@ function showGuestUserForm() {
|
||||
echo '<option value="0">Require email confirmation? — No</option>';
|
||||
echo '<option value="1">Require email confirmation? — Yes</option>';
|
||||
echo '</select> ';
|
||||
echo '<select name="require_unique_username" size="1">';
|
||||
echo '<option value="0">Username — Any</option>';
|
||||
echo '<option value="1">Username — Unique</option>';
|
||||
echo '</select> ';
|
||||
echo '<button type="submit">Register</button>';
|
||||
echo '</form>';
|
||||
|
||||
|
Reference in New Issue
Block a user