Download the PHP package struktal/struktal-auth without Composer
On this page you can find all versions of the php package struktal/struktal-auth. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download struktal/struktal-auth
More information about struktal/struktal-auth
Files in struktal/struktal-auth
Package struktal-auth
Short Description PHP library for user authentication in Struktal applications
License MIT
Informations about the package struktal-auth
Struktal-Auth
This is a PHP library that provides a basic structure for authentication and authorization in Struktal applications.
Installation
To install this library, include it in your project using Composer:
GenericUser
This library uses the struktal/struktal-orm library to provide a GenericUser class that can be used as a base class for your own user model object.
Besides the standard id, created, and updated attributes, it also provides the following fields:
username(string) - The username of the userpassword(string) - The hashed password of the useremail(string) - The email address of the user (encrypted)emailVerified(boolean) - Whether the user's email address has been verifiedpermissionLevel(integer) - The permission level of the user (0 by default, can be any integer value)oneTimePassword(string) - A one-time password for the user which can be used for email verification or password resetoneTimePasswordExpiration(DateTimeImmutable) - The expiration date and time of the one-time password
You can create the database table with the following SQL statement:
Usage
At first, create a custom PermissionLevel enum to define the permission levels of your application:
Then, create a custom user object that extends the GenericUser class
and a custom data access object (DAO) that extends the GenericUserDAO class
If your user object contains custom fields that have to be set when registering the user, you should also override the register() method in the UserDAO class.
Take a look at the GenericUserDAO class to see how the method is implemented there.
Finally, you can create the database table.
To do so, orientate yourself on the example above for the GenericUser table.
You have to extend the SQL code with your custom fields and change the table name to the name of your custom user object (e.g. User).
In your application's startup script, you then have to register the custom user object and DAO:
Registering new Users
To register a new user, use the register() function, which creates a new user object, sets the required fields with the passed parameters, and saves it to the database.
Login and Logout
To check the account credentials when a user tries to log in, you can use the login() method from the UserDAO class.
It returns the user object if the credentials are valid or a LoginError to describe the error that occurred.
To set the session variable for a user to be logged in, you can use the login() method from the \struktal\Auth\Auth class.
You have to pass a corresponding GenericUser object to the method.
To log out a user, you can use the logout() method from the \struktal\Auth\Auth class, which deletes the session variable for the logged-in user.
Required Login
If you want a user to be logged in when accessing a specific page of your application, use the enforceLogin() method from the \struktal\Auth\Auth class immediately at the beginning of your script.
It takes parameters for the minimum required permission level (as an enum from your PermissoinLevel enum) and a redirect URL to which the user will be redirected if they are not logged in or do not have the required permission level.
Optional Login
If you only want to retrieve the currently logged-in user without enforcing a login, you can use the getLoggedInUser() method from the \struktal\Auth\Auth class.
Dependencies
This library uses the following dependencies:
- ext-pdo
- struktal/struktal-orm - GitHub: Struktal/sturktal-orm, licensed under MIT license
License
This software is licensed under the MIT license. See the LICENSE file for more information.