wscore / auth
a simple authenticate package.
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is not auto-updated.
Last update: 2025-01-04 21:19:37 UTC
README
A simple authentication package.
License
MIT License
PSR
PSR-1, PSR-2, and PSR-4.
Installation
composer require "wscore/auth: ^0.3"
Getting Started
Auth
requires UserProviderInterface
object to access to user information.
$auth = new Auth(new UserProvider);
To authenticate a user, get user-id ($id
) and user-password ($pw
) from a login form, and
if ($auth->login($id, $pw)) { echo 'login success!'; }
to check for login later on,
$auth->isLogin();
You can retrieve login information such as;
$user = $auth->getLoginUser(); // login user entity returned by UserProvider's getUserInfo() method. $mail = $auth->getLoginId(); // get login user's id. maybe an email?
Force Login
forceLogin
method allow to login as a user without a password,
for purposes, such as system administration.
$auth->forceLogin($id);
then, you can check if the login is force or not.
$auth->isLoginBy(Auth::BY_FORCED); // check for login method.
UserProvider
The Auth
requires a user provider object implementing UserProviderInterface
.
The interface has 4 APIs; that are
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.
Remember-Me Option
To use Remember-me option, use setRememberMe
method, as
$auth = new Auth(...); $auth->setRememberMe(new MyRememberMe());
$remember
object implementingRememberMeInterface
,RememberCookie
object,
then, when login, supply 3rd argument when login
as
$auth->login($id, $pw, true);
to save the $id
and a remember-me token to cookie if login is successful.