marcosdipaolo/auth
Composer 安装命令:
composer require marcosdipaolo/auth
包简介
An PHP Authentication Package
README 文档
README
A basic PHP Authentication Package
The Auth constructor receives as a unique argument a PDO instance connected to your app's database.
This connection is necessary to check credentials and register users. The rest of the operations can be performed without passing anything, like logging in or logging out a user, checking if there's a user logged in, or getting the logged user.
The login and logout method just puts or remove the user in the user session's key. In order to do so Auth makes use of the marcosdipaolo/session package.
Usage
Configuration
/** @var PDO $pdoInstance */ $auth = new MDP\Auth\Auth($pdoInstance); // Setting the users table name // The table the Auth package is going to interact with: $auth->setUsersTableName('members'); // default: 'users' // Setting the table fields the package should manage $auth->setEmailField('email_address'); // default: 'email' $auth->setPasswordField('pass'); // default: 'password' $auth->setUsernameField('alias'); // default: 'username' // You can do it in one time with the setUsersTableFields method $auth->setUsersTableFields([ 'usernameField' => 'username', 'passwordField' => 'password', 'emailField' => 'email' ]); // boolean, true if at least one field was set // Set the column that is going to be evaluated for logging in $auth->setLoginField('email'); // default: 'email'
Login and register
In order to log a user in, you must use the login method passing as an argument an instance of a user, whose class should be implementing the \MDP\Auth\Autheticatable interface.
/** @var \MDP\Auth\Auth $auth */// boolean;v /** @var MDP\Auth\Authenticatable $user */ $auth->login($user); // logs in a user
Here checking credentials, loggin out, checking if there's a logged user, getting the logged user and registering.
/** @var \MDP\Auth\Auth $auth */ // check credentials $auth->check('yourField', 'youPassword'); // Register a user $auth->register('john_doe', 'john@doe.com', 'myPassword'); // bool (true if successful) $auth->logout(); // logs out whoever was logged in // Checks if there's someone logged in $auth->isUserLoggedIn(); // bool // Returns the logged user $auth->user(); // MDP\Auth\Authenticatable logged user | null
Helper auth()
The auth() helper return an instance of the Auth class.
Since many operations do not require a connection to the db, you are not required of passing the instance of the PDO class as an argument.
If you need to perform operations that requires a db connection, like register, or check you need to pass the PDO instance auth($pdo).
统计信息
- 总下载量: 53
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-09