PHPackages                             struktal/struktal-auth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. struktal/struktal-auth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

struktal/struktal-auth
======================

PHP library for user authentication in Struktal applications

4.0.1(4mo ago)01.7k↑192.3%1MITPHPPHP &gt;=8.2.0CI passing

Since Jun 26Pushed 4mo agoCompare

[ Source](https://github.com/Struktal/struktal-auth)[ Packagist](https://packagist.org/packages/struktal/struktal-auth)[ RSS](/packages/struktal-struktal-auth/feed)WikiDiscussions main Synced yesterday

READMEChangelog (10)Dependencies (1)Versions (12)Used By (1)

Struktal-Auth
=============

[](#struktal-auth)

This is a PHP library that provides a basic structure for authentication and authorization in Struktal applications.

Installation
============

[](#installation)

To install this library, include it in your project using Composer:

```
composer require struktal/struktal-auth
```

GenericUser
===========

[](#genericuser)

This library uses the [struktal/struktal-orm](https://github.com/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 user
- `password` (string) - The hashed password of the user
- `email` (string) - The email address of the user (encrypted)
- `emailVerified` (boolean) - Whether the user's email address has been verified
- `permissionLevel` (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 reset
- `oneTimePasswordExpiration` (DateTimeImmutable) - The expiration date and time of the one-time password

You can create the database table with the following SQL statement:

```
CREATE TABLE IF NOT EXISTS `GenericUser` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(255) NOT NULL,
    `password` VARCHAR(255) NOT NULL,
    `email` VARCHAR(255) NOT NULL,
    `emailVerified` TINYINT(1) NOT NULL DEFAULT 0,
    `permissionLevel` INT NOT NULL,
    `oneTimePassword` VARCHAR(255) NULL,
    `oneTimePasswordExpiration` DATETIME NULL,
    `created` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    PRIMARY KEY (`id`),
    UNIQUE KEY (`username`),
    UNIQUE KEY (`email`),
    UNIQUE KEY (`oneTimePassword`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```

Usage
=====

[](#usage)

At first, create a custom `PermissionLevel` enum to define the permission levels of your application:

```
enum PermissionLevel: int implements \struktal\Auth\PermissionLevel {
    // Examples:
    case USER = 0;
    case ADMIN = 1;

    public function value(): int {
        return $this->value;
    }
}
```

Then, create a custom user object that extends the `GenericUser` class

```
class User extends \struktal\ORM\GenericUser {
    #[\struktal\ORM\InheritedType(PermissionLevel::class)]
    public ?PermissionLevel $permissionLevel = null;

    // You can add custom methods or properties here if needed
}
```

and a custom data access object (DAO) that extends the `GenericUserDAO` class

```
class UserDAO extends \struktal\ORM\GenericUserDAO {
    // You can add custom methods or properties here if needed
}
```

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:

```
\struktal\Auth\Auth::setUserObjectName(User::class);
```

Registering new Users
---------------------

[](#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
----------------

[](#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
--------------

[](#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.

```
$auth = new \struktal\Auth\Auth();
$user = $auth->enforceLogin(PermissionLevel::USER, Router->generate("nologin"));
```

Optional Login
--------------

[](#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.

```
$auth = new \struktal\Auth\Auth();
$user = $auth->getLoggedInUser();
```

Dependencies
============

[](#dependencies)

This library uses the following dependencies:

- **ext-pdo**
- **struktal/struktal-orm** - GitHub: [Struktal/sturktal-orm](https://github.com/Struktal/struktal-orm), licensed under [MIT license](https://github.com/Struktal/struktal-orm/blob/main/LICENSE)

License
=======

[](#license)

This software is licensed under the MIT license. See the [LICENSE](LICENSE) file for more information.

###  Health Score

43

—

FairBetter than 89% of packages

Maintenance76

Regular maintenance activity

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~24 days

Recently: every ~40 days

Total

11

Last Release

132d ago

Major Versions

1.1.0 → 2.0.02025-06-30

2.0.2 → 3.0.02025-09-12

3.1.1 → 4.0.02025-10-05

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/49905418?v=4)[Jens Ostertag](/maintainers/JensOstertag)[@JensOstertag](https://github.com/JensOstertag)

---

Top Contributors

[![JensOstertag](https://avatars.githubusercontent.com/u/49905418?v=4)](https://github.com/JensOstertag "JensOstertag (12 commits)")

---

Tags

Authenticationstrukal

### Embed Badge

![Health badge](/badges/struktal-struktal-auth/health.svg)

```
[![Health](https://phpackages.com/badges/struktal-struktal-auth/health.svg)](https://phpackages.com/packages/struktal-struktal-auth)
```

###  Alternatives

[tymon/jwt-auth

JSON Web Token Authentication for Laravel and Lumen

11.7k51.8M371](/packages/tymon-jwt-auth)[league/oauth2-server

A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.

6.7k147.0M290](/packages/league-oauth2-server)[league/oauth2-client

OAuth 2.0 Client Library

3.8k128.7M1.3k](/packages/league-oauth2-client)[google/auth

Google Auth Library for PHP

1.4k294.2M218](/packages/google-auth)[pragmarx/google2fa

A One Time Password Authentication package, compatible with Google Authenticator.

2.0k97.9M239](/packages/pragmarx-google2fa)[paragonie/sodium_compat

Pure PHP implementation of libsodium; uses the PHP extension if it exists

931145.2M177](/packages/paragonie-sodium-compat)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
