|
1 | | -# PHP-Auth |
| 1 | +# PHP-Auth |
| 2 | +A complete user authentication library written in PHP |
| 3 | + |
| 4 | +## Installation |
| 5 | +Use the package manager [composer](https://getcomposer.org) to install the library. |
| 6 | +```bash |
| 7 | +composer require riculum/php-auth |
| 8 | +``` |
| 9 | + |
| 10 | +## Initial setup |
| 11 | +### Credentials |
| 12 | +The basic database settings can be set through environment variables. Add a `.env` file in the root of your project. Make sure the `.env` file is added to your `.gitignore` so it is not checked-in the code. By default, the library looks for the following variables: |
| 13 | + |
| 14 | +* DB_HOST |
| 15 | +* DB_NAME |
| 16 | +* DB_USERNAME |
| 17 | +* DB_PASSWORD |
| 18 | +* DB_PREFIX |
| 19 | + |
| 20 | +More information how to use environment variables [here](https://github.com/vlucas/phpdotenv) |
| 21 | + |
| 22 | +### Database |
| 23 | +```sql |
| 24 | +CREATE TABLE IF NOT EXISTS user ( |
| 25 | + id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, |
| 26 | + uuid VARCHAR(50) NOT NULL UNIQUE, |
| 27 | + firstname VARCHAR(50) NOT NULL, |
| 28 | + lastname VARCHAR(50) NOT NULL, |
| 29 | + email VARCHAR(100) NOT NULL UNIQUE, |
| 30 | + password VARCHAR(255) NOT NULL, |
| 31 | + token VARCHAR(255) NOT NULL, |
| 32 | + attempts TINYINT NOT NULL DEFAULT 0, |
| 33 | + online TINYINT NOT NULL DEFAULT 0, |
| 34 | + verified TINYINT NOT NULL DEFAULT 0, |
| 35 | + enabled TINYINT NOT NULL DEFAULT 1, |
| 36 | + updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 37 | + createdAt DATETIME DEFAULT CURRENT_TIMESTAMP |
| 38 | +) |
| 39 | +``` |
| 40 | + |
| 41 | +*Note: We recommend to set a database prefix* |
| 42 | + |
| 43 | +### Configuration |
| 44 | +Import vendor/autoload.php and load the `.env` settings |
| 45 | +```php |
| 46 | +require_once 'vendor/autoload.php'; |
| 47 | + |
| 48 | +use Auth\Core\Authentication as Auth; |
| 49 | + |
| 50 | +$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); |
| 51 | +$dotenv->load(); |
| 52 | +``` |
| 53 | + |
| 54 | +## Usage |
| 55 | +### Registration |
| 56 | +Use an associative array with user data to register a new user |
| 57 | +```php |
| 58 | +$user = array( |
| 59 | + 'firstname' => 'John', |
| 60 | + 'lastname' => 'Doe', |
| 61 | + 'email' => ' [email protected]', //must be unique |
| 62 | + 'password' => '$2y$10$jNtkQSKNni2ELyoi9Y/lpedy7v92FYzqz5ePm1M6jPGY9hb8TCmAq', |
| 63 | + 'token' => md5(uniqid(rand(), true)) |
| 64 | +); |
| 65 | + |
| 66 | +try { |
| 67 | + echo Auth::register($user); |
| 68 | +} catch (UserAlreadyExistsException $e) { |
| 69 | + echo 'User with the specified email address already exists'; |
| 70 | +} catch (Exception $e) { |
| 71 | + echo "Something went wrong"; |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Login |
| 76 | +```php |
| 77 | +try { |
| 78 | + Auth::login(' [email protected]', '123456'); |
| 79 | + echo 'Login successful'; |
| 80 | +} catch (InvalidEmailException | InvalidPasswordException $e) { |
| 81 | + echo 'Email or Password are wrong'; |
| 82 | +} catch(UserNotEnabledException $e) { |
| 83 | + echo 'User account has been deactivated'; |
| 84 | +} catch (TooManyAttemptsException $e) { |
| 85 | + echo 'Too many failed login attempts'; |
| 86 | +} catch (Exception $e) { |
| 87 | + echo "Something went wrong"; |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Verify |
| 92 | +```php |
| 93 | +if (Auth::verify()) { |
| 94 | + echo "Authorization successful"; |
| 95 | +} else { |
| 96 | + echo "Authorization failed"; |
| 97 | +} |
| 98 | +``` |
| 99 | +### Logout |
| 100 | +```php |
| 101 | +try { |
| 102 | + Auth::logout(); |
| 103 | +} catch (Exception $e) { |
| 104 | + echo 'Something went wrong'; |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## Bugreport & Contribution |
| 109 | +If you find a bug, please either create a ticket in github, or initiate a pull request |
| 110 | + |
| 111 | +## Versioning |
| 112 | +We adhere to semantic (major.minor.patch) versioning (https://semver.org/). This means that: |
| 113 | + |
| 114 | +* Patch (x.x.patch) versions fix bugs |
| 115 | +* Minor (x.minor.x) versions introduce new, backwards compatible features or improve existing code. |
| 116 | +* Major (major.x.x) versions introduce radical changes which are not backwards compatible. |
| 117 | + |
| 118 | +In your automation or procedure you can always safely update patch & minor versions without the risk of your application failing. |
| 119 | + |
| 120 | + |
0 commit comments