PHPackages                             odevnet/dulceauth - 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. odevnet/dulceauth

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

odevnet/dulceauth
=================

PHP user management library that facilitates user registration and authentication, as well as the management of their roles and permissions. It is designed for small and medium-sized applications that require a robust, efficient and extensible solution.

v2.0.2(9mo ago)117MITPHPPHP &gt;=8.1.0

Since May 15Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/odevnet/dulceAuth)[ Packagist](https://packagist.org/packages/odevnet/dulceauth)[ Docs](https://github.com/odevnet/dulceAuth/)[ RSS](/packages/odevnet-dulceauth/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (5)Used By (0)

[![GitHub tag (latest by date)](https://camo.githubusercontent.com/35f1b6d9ed2c26cd20672cfcd051fb8382f57097afb498be97df386d8cf3d369/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f6f6465766e65742f64756c636541757468)](https://camo.githubusercontent.com/35f1b6d9ed2c26cd20672cfcd051fb8382f57097afb498be97df386d8cf3d369/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f6f6465766e65742f64756c636541757468)[![GitHub](https://camo.githubusercontent.com/14835ee30aa1895e5198776756cbe8202c3244600386e875d40c3fc61950d9b4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6f6465766e65742f64756c636541757468)](https://camo.githubusercontent.com/14835ee30aa1895e5198776756cbe8202c3244600386e875d40c3fc61950d9b4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6f6465766e65742f64756c636541757468)[![Static Badge](https://camo.githubusercontent.com/1a10ea6365cfab4e3b0a33c0019f4f17fdaffa3bd38c3bf2678f40035443d9b3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62795f5048502d2532333633373546323f7374796c653d666c6174266c6f676f3d504850266c6f676f436f6c6f723d253233363337354632266c6f676f53697a653d32377078266c6162656c436f6c6f723d253233666666)](https://camo.githubusercontent.com/1a10ea6365cfab4e3b0a33c0019f4f17fdaffa3bd38c3bf2678f40035443d9b3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62795f5048502d2532333633373546323f7374796c653d666c6174266c6f676f3d504850266c6f676f436f6c6f723d253233363337354632266c6f676f53697a653d32377078266c6162656c436f6c6f723d253233666666)[![Static Badge](https://camo.githubusercontent.com/e0b2cf92a98e2cd30f3eb919042a8d557433d2269e7811b958b82f4f5f3fcbe2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c45454d452d455350412543332539314f4c2d5245443f7374796c653d666c6174266c6162656c436f6c6f723d25323346353139304225323026636f6c6f723d253233463444413336266c696e6b3d68747470733a2f2f6769746875622e636f6d2f6f6465766e65742f64756c6365417574682f626c6f622f6d61696e2f4c45454d452e6d64)](https://github.com/odevnet/dulceAuth/blob/main/LEEME.md)

What is "dulceAuth"?
====================

[](#what-is-dulceauth)

**DulceAuth** is a PHP user management library that simplifies user registration and authentication, as well as the management of roles and permissions. It is designed for small to medium-sized applications that need a robust, efficient, and extensible solution.

Some features include:

- You can register one or multiple users at once.
- Controls access to different parts of the application by assigning roles and permissions.
- Integrates Laravel's "Eloquent ORM" to facilitate working with the database.
- Verifies user accounts by generating and validating tokens.
- Resets passwords.
- Supports easy email sending.
- Utilizes sessions and allows verification of user sessions.
- Captures and logs any errors that may occur.
- Easily extensible and customizable thanks to its modular architecture.
- Manages and facilitates dependency injection through the use of a "service container."

Table of Contents
=================

[](#table-of-contents)

1. [Installation and Usage](#installation-and-usage)
    1. [Through GitHub](#github)
    2. [Through Composer](#composer)
    3. [Library Usage](#usage)
2. [Configuration](#configuration)
    1. [Database](#database)
    2. [Config.php](#config-file)
    3. [Database Configuration File](#database-configuration-file)
    4. [JSON Files](#json-files)
3. [Exceptions](#exception-handling)
4. [Logger Class](#logger-class)
5. [User Registration](#register-user)
    1. [Account Verification](#account-verification)
    2. [Account Verification. Part Two.](#account-verification-part-two)
    3. [An Exceptional Case](#an-exceptional-case-or-not)
6. [Constantes personalizadas](#custom_verification_email_url-y-custom_forgot_password_email_url)
7. [Login](#login)
8. [Users](#users)
    1. [Does it Exist?](#does-the-user-exist)
    2. [Edit User](#editing-users)
    3. [Delete User](#deleting-a-user)
    4. [Create Users](#creating-a-new-user)
    5. [Change Password](#changing-a-users-password)
    6. [Recover Password/Forgot Password](#password-recovery)
9. [Roles and Permissions](#roles-and-permissions)
    1. [Create a Role](#create-a-new-role)
    2. [Edit a Role](#edit-a-role)
    3. [Delete a Role](#delete-a-role)
    4. [Assign Roles to Users](#assign-roles-to-users)
    5. [Permissions](#permissions)
        1. [Create a Permission](#create-permission)
        2. [Edit a Permission](#edit-permission)
        3. [Delete a Permission](#delete-a-permission)
        4. [Assign a Permission to a Role](#assign-permissions-to-roles)
        5. [Remove a Permission from a Role](#remove-permission-from-a-role)
10. [Roles and Permissions. Part 2](#roles-and-permissions-v2)
11. [Authorization](#authorization)
12. [Sessions](#sessions)
    1. [Session Duration](#session-duration)
13. [Email Class](#dulcemail)
14. [Creating services](#create-a-service)

Claro, aquí está la traducción respetando el formato de markdown:

Installation and Usage
======================

[](#installation-and-usage)

The only requirements are:

- MySQL database as a minimum (not tested on others)
- PHP version &gt;= 8.2.0

Github
------

[](#github)

Open the command prompt (cmd) on Windows or the terminal of your operating system and navigate to the folder where you want to clone the repository:

For example, in the console, type:

`cd path/to/your/directory`

And then clone the repository by running in the console:

`git clone https://github.com/odevnet/dulceAuth.git`

Once cloned, with the console open and in the project directory, run:

`composer install` to automatically install the necessary dependencies.

Composer
--------

[](#composer)

In a terminal and with composer installed, run the following command:

`composer require odevnet/dulceauth`

This will install the library and all necessary dependencies.

You then **have two options** to complete the installation.

1. Open the *composer.json* file for your project, i.e., where you downloaded the library, and you'll see something like this:

```
{
    "require": {
        "odevnet/dulceauth": "dev-main"
    }
}
```

Well, you should leave it this way:

```
{
    "require": {
        "odevnet/dulceauth": "dev-main"
    },
    "scripts": {
        "post-install-cmd": [
            "Install\\Install::createDirectoryStructure"
        ]
    }
}
```

Next, in the terminal or console, type: `composer dump-autoload` and press Enter.

Finally, run `composer install` and the installation file (Install.php) will automatically run, creating the files *config/config.php*, *config/config-db.php*, *config/verification\_email.json*, *config/forgot\_password\_email.json*, and *logs/log\_file.log*.

2. Skip step one and, once the library has downloaded, run the command:

`php vendor/odevnet/dulceauth/installer.php`

This command will create the folders and copy the necessary configuration files. Just like step one, the structure should look like this:

your-project/

```
-config/

    -config.php

    -config-db.php

    -verification_email.json

    -forgot_password_email.json

-logs/

    -log_file.log

-vendor/

    -odevnet/

    -dulceauth/

```

> **Important:** This second method is preferred, as it's faster and easier.

Don't worry, you can modify the routes later if you don't like them... ;-)

Usage
-----

[](#usage)

Once you have *dulceAuth* downloaded and [configured](#configuration), simply include and instantiate the library to use it like this:

```
require __DIR__ . '/vendor/autoload.php';

$config = __DIR__ . '/config/config.php'; // path where your configuration file is located
$databaseConfig = __DIR__ . '/config/config-db.php'; // path where your database configuration file is located

$dulceAuth = new src\DulceAuth([$config, $databaseConfig]);
```

And from here, you will be able to use each of the available methods provided by the library ;)

Configuration
=============

[](#configuration)

Database
--------

[](#database)

Use the following [table structure](db_tables.sql "Required Tables") for a MySQL database. These are the minimum required tables and fields. If you already have a **users** table, you will need to add the following fields: **name, email, password, created\_at, updated\_at, verified, and visibility**. For example, if your table is named *"usuarios"* and you already have a field called *"username"* for the user's name, you will need to rename that field to *name* and the table name to *users*. You should do the same for any other fields that differ.

Config File
-----------

[](#config-file)

During installation, a **config.php** file has been created which in itself is quite descriptive about what each configuration option does and which ones we can modify and which ones we cannot.

For now, it's basic but functional.

Mainly, you only need to modify the constants *WEB\_PAGE* and *EMAIL\_FROM*. We can leave the rest as is, however, if we want more customization, we can configure the paths of the JSON or .log files if we consider it necessary.

```
// Define the project base route
define('DULCE_AUTH_BASE_DIR', dirname(__DIR__)); // Return to the root of the project from src/config/

// Define common constants here
define('DULCE_AUTH_WEB_PAGE', 'yourwebsite.com'); // without http(s), without www and without ending in /
// some examples: define('WEB_PAGE', 'yourwebsite.com'); or define('WEB_PAGE', 'yourwebsite.com/myFolder');
define('DULCE_AUTH_EMAIL_FROM', 'admin@yourwebsite.com');

// Error log
define('DULCE_AUTH_LOG_FILE', BASE_DIR . '/logs/log_file.log');

// A little configuration about emails...
define('DULCE_AUTH_VERIFICATION_EMAIL_JSON_FILE', BASE_DIR . '/config/verification_email.json'); // json template for verification email. Edit the text as you like
define('DULCE_AUTH_VERIFICATION_PAGE_URL', 'verification.php'); // default file where the verification email data is captured

define('DULCE_AUTH_FORGOT_PASSWORD_EMAIL_JSON_FILE', BASE_DIR . '/config/forgot_password_email.json'); // json template for forgotten password email. Edit the text as you like
define('DULCE_AUTH_FORGOT_PASSWORD_PAGE_URL', 'forgot.php'); // default file where the email data (token and user id) is captured

// Roles. At the moment do not modify anything!!
define('DULCE_AUTH_DEFAULT_ROLE', 'User'); // default role
define('DULCE_AUTH_DEFAULT_VISIBILITY', 'public'); // default profile visibility

// Accounts
define('DULCE_AUTH_VERIFIED', '0'); // 0 = unverified account, requires email validation. 1 = verified
define('DULCE_AUTH_MAX_PASSWORD_CHANGES', 3); // password changes allowed per year

// Sessions
define('DULCE_AUTH_SESSION_EXPIRATION', 60 * 60); // session lifetime.
//For 1 day: define(DULCE_AUTH_SESSION_EXPIRATION', 60 * 60 * 24);
//For 2 days: define('DULCE_AUTH_SESSION_EXPIRATION', 60 * 60 * 24 * 2);
//For 7 days: define('DULCE_AUTH_SESSION_EXPIRATION', 60 * 60 * 24 * 7);
//For 1 hour: define('DULCE_AUTH_SESSION_EXPIRATION', 60 * 60);
```

Database Configuration File
---------------------------

[](#database-configuration-file)

During installation, a file called **config-db.php** was created which is used to configure the database data.

```
