|
| 1 | +Introduction |
| 2 | +------------ |
| 3 | + |
| 4 | +XOUser is a skeleton module for user authentication and registration for Zend Framework 2. This is for making a user module very fast by extending its available functionality. |
| 5 | + |
| 6 | +XOUser stores session data in database. This is mainly a combination of Zend\Db, Zend\Session and Zend\Authentication for managing users persistently. |
| 7 | + |
| 8 | + |
| 9 | +Features |
| 10 | +-------- |
| 11 | + |
| 12 | +* User login - authenticate via username or email (by specifying one of these two). |
| 13 | +* User registration. |
| 14 | +* User change-password. |
| 15 | +* Forms protected against CSRF. |
| 16 | + |
| 17 | +Installation |
| 18 | +------------ |
| 19 | + |
| 20 | +### Database Config: |
| 21 | + |
| 22 | +XOUser expects and assumes you have a valid database configuration under a top key named `db`. |
| 23 | + |
| 24 | +### Database Tables: |
| 25 | + |
| 26 | +XOUser expects two database tables named `users` and `session` for managing users and sessions respectively: |
| 27 | + |
| 28 | +```sql |
| 29 | +CREATE TABLE IF NOT EXISTS `users` ( |
| 30 | + `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 31 | + `email` varchar(100) DEFAULT NULL, |
| 32 | + `username` varchar(100) DEFAULT NULL, |
| 33 | + `password` varchar(60) DEFAULT NULL, |
| 34 | + `modifiedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 35 | + `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 36 | + PRIMARY KEY(`id`), |
| 37 | + KEY `idx_email` (`email`), |
| 38 | + KEY `idx_username` (`username`) |
| 39 | +) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
| 40 | +INSERT INTO `users` ( `id`, `email`, `username`, `password`, `modifiedAt`, `createdAt`) VALUES ( 1, '[email protected]', 'admin', '$2y$10$iMDN8kS81DAdHy9/zNd3we2ChPwhy2bTkVIsCyHpNtaNZl9zUuyxG', '0000-00-00 00:00:00', '0000-00-00 00:00:00'); |
| 41 | + |
| 42 | +CREATE TABLE IF NOT EXISTS `session` ( |
| 43 | + `id` char(32) NOT NULL, |
| 44 | + `name` char(32) NOT NULL, |
| 45 | + `modified` int(11) DEFAULT NULL, |
| 46 | + `lifetime` int(11) DEFAULT NULL, |
| 47 | + `data` text, |
| 48 | + PRIMARY KEY (`id`), |
| 49 | + KEY `idx_name` (`name`) |
| 50 | +) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
| 51 | + |
| 52 | +``` |
| 53 | + |
| 54 | +### Session Config: |
| 55 | + |
| 56 | +If you want to set custom configuration for handling session, you should do that under top key `session_config`. If you do not already have a custom configuration for your session, put the following in `./config/module.config.php`: |
| 57 | + |
| 58 | +```php |
| 59 | +<?php |
| 60 | + |
| 61 | + 'session_config' => array( |
| 62 | + 'name' => 'session_name', |
| 63 | + 'use_cookies' => true, |
| 64 | + 'cookie_lifetime' => 0, |
| 65 | + 'gc_maxlifetime' => 3600, |
| 66 | + ), |
| 67 | + |
| 68 | +``` |
| 69 | + |
| 70 | +### Usage: |
| 71 | + |
| 72 | +Use the following snippet of code in your controller method to manage login action. For more information, you should check the `LoginController`'s `processAction` method. |
| 73 | + |
| 74 | +```php |
| 75 | +<?php |
| 76 | + |
| 77 | + $auth = $this->getAuthService() |
| 78 | + ->getAdapter() |
| 79 | + ->setIdentity($data['username']) |
| 80 | + ->setCredential($data['password']) |
| 81 | + ->setIdentityType('username'); // This can only be 'username' and 'email' |
| 82 | + |
| 83 | + $result = $this->getAuthService()->authenticate(); |
| 84 | + |
| 85 | + if ($result->isValid()) { |
| 86 | + // Do something |
| 87 | + } else { |
| 88 | + // Do something |
| 89 | + } |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +Next up, just use this over and over again where you need: |
| 94 | + |
| 95 | +```php |
| 96 | +<?php |
| 97 | + |
| 98 | + if (!$this->getAuthService()->hasIdentity()) { |
| 99 | + return $this->redirect()->toRoute('auth'); |
| 100 | + } |
| 101 | +``` |
| 102 | + |
| 103 | +Available Routes |
| 104 | +---------------- |
| 105 | + |
| 106 | +```php |
| 107 | + |
| 108 | + /auth |
| 109 | + /auth/login |
| 110 | + /auth/signup |
| 111 | + /auth/change-password |
| 112 | + /auth/logout |
| 113 | + |
| 114 | +``` |
| 115 | + |
| 116 | +Go to your site: http://yoursite.dev/auth and you should see a login page. |
| 117 | + |
| 118 | + |
| 119 | +Login |
| 120 | +----- |
| 121 | + |
| 122 | +```php |
| 123 | +<?php |
| 124 | + |
| 125 | + username: admin |
| 126 | + password: 12345678 |
| 127 | + |
| 128 | +``` |
| 129 | + |
| 130 | +Password Hash Caution |
| 131 | +--------------------- |
| 132 | + |
| 133 | +**DO NOT CHANGE THE PASSWORD HASH SETTINGS FROM THEIR DEFAULTS** unless you |
| 134 | +have fully understood exactly what and why you are doing! |
| 135 | + |
| 136 | +ZF2 Components |
| 137 | +-------------- |
| 138 | + |
| 139 | +The following ZF2 components are considerably used in XOUser module: |
| 140 | + |
| 141 | +* [Zend/Authentication](https://framework.zend.com/manual/2.4/en/modules/zend.authentication.intro.html) |
| 142 | +* [Zend/Crypt](https://framework.zend.com/manual/2.4/en/modules/zend.crypt.introduction.html) |
| 143 | +* [Zend/Db](https://framework.zend.com/manual/2.4/en/modules/zend.db.adapter.html) |
| 144 | +* [Zend/Filter](https://framework.zend.com/manual/2.4/en/modules/zend.filter.html) |
| 145 | +* [Zend/Form](https://framework.zend.com/manual/2.4/en/modules/zend.form.intro.html) |
| 146 | +* [Zend/InputFilter](https://framework.zend.com/manual/2.4/en/modules/zend.input-filter.intro.html) |
| 147 | +* [Zend/Json](https://framework.zend.com/manual/2.4/en/modules/zend.json.introduction.html) |
| 148 | +* [Zend/ModuleManager](https://framework.zend.com/manual/2.4/en/modules/zend.module-manager.intro.html) |
| 149 | +* [Zend/Mvc](https://framework.zend.com/manual/2.4/en/modules/zend.mvc.intro.html) |
| 150 | +* [Zend/ServiceManager](https://framework.zend.com/manual/2.4/en/modules/zend.service-manager.html) |
| 151 | +* [Zend/Session](https://framework.zend.com/manual/2.4/en/modules/zend.session.config.html) |
| 152 | +* [Zend/Validator](https://framework.zend.com/manual/2.4/en/modules/zend.validator.html) |
| 153 | +* [Zend/View](https://framework.zend.com/manual/2.4/en/modules/zend.view.quick-start.html) |
| 154 | + |
| 155 | +License |
| 156 | +------- |
| 157 | + |
| 158 | +This ZF2 module released under MIT license. |
0 commit comments