a simple authenticate package.
A simple authentication package., (*1)
MIT License, (*2)
PSR-1, PSR-2, and PSR-4., (*3)
composer require "wscore/auth: ^0.3"
Auth
requires UserProviderInterface
object to access to user information., (*4)
$auth = new Auth(new UserProvider);
To authenticate a user, get user-id ($id
) and user-password ($pw
) from a login form, and, (*5)
if ($auth->login($id, $pw)) { echo 'login success!'; }
to check for login later on,, (*6)
$auth->isLogin();
You can retrieve login information such as;, (*7)
$user = $auth->getLoginUser(); // login user entity returned by UserProvider's getUserInfo() method. $mail = $auth->getLoginId(); // get login user's id. maybe an email?
forceLogin
method allow to login as a user without a password,
for purposes, such as system administration., (*8)
$auth->forceLogin($id);
then, you can check if the login is force or not., (*9)
$auth->isLoginBy(Auth::BY_FORCED); // check for login method.
The Auth
requires a user provider object implementing UserProviderInterface
.
The interface has 4 APIs; that are, (*10)
getUserById($id)
: for retrieving a user entity for $id
(a login user). getUserByIdAndPw($id, $pw)
: for retrieving a user entity for $id
and valid $pw
. getUserType()
: for retrieving a key-string to identify the user-provider. To use Remember-me option, use setRememberMe
method, as, (*11)
$auth = new Auth(...); $auth->setRememberMe(new MyRememberMe());
$remember
object implementing RememberMeInterface
, RememberCookie
object, then, when login, supply 3rd argument when login
as, (*12)
$auth->login($id, $pw, true);
to save the $id
and a remember-me token to cookie if login is successful., (*13)