PHPackages                             liveintent/laravel-common - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. liveintent/laravel-common

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

liveintent/laravel-common
=========================

Shared tools for laravel projects

v1.2.0(1y ago)014.0k↓48.2%PHPPHP ^8.0|^8.1

Since Sep 13Pushed 1y ago4 watchersCompare

[ Source](https://github.com/LiveIntent/laravel-common)[ Packagist](https://packagist.org/packages/liveintent/laravel-common)[ Docs](https://github.com/liveintent/laravel-common)[ RSS](/packages/liveintent-laravel-common/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (33)Used By (0)

🧰 Laravel Common
================

[](#-laravel-common)

[![Latest Version on Packagist](https://camo.githubusercontent.com/daec5b3bcc111e225a22af95adbdb37283b8a5bb23c6999f324c25551d3a18fc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c697665696e74656e742f6c61726176656c2d636f6d6d6f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/liveintent/laravel-common)[![GitHub Tests Action Status](https://camo.githubusercontent.com/b6b58f446462a3ae3334960b3417a2172ea6495f9e070c040ef04dbc0ff77063/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c697665696e74656e742f6c61726176656c2d636f6d6d6f6e2f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/liveintent/laravel-common/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/8a4260099051debf6e66c74eaceb53363c7192f638d60e295b749b67a06ea43b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c697665696e74656e742f6c61726176656c2d636f6d6d6f6e2f72756e2d6c696e743f6c6162656c3d636f64652532307374796c65)](https://github.com/liveintent/laravel-common/actions?query=workflow%3Arun-lint+branch%3Amain)

This package contains a collection of shared helpful utilities used across our Laravel projects.

What's Included
---------------

[](#whats-included)

- authentication guards and user providers
- http request logging
- notification sending via normani
- common middleware
- common macro definitions
- generator stubs for quick code scaffolding
- other cool things

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require liveintent/laravel-common
```

### Adding authentication to your API

[](#adding-authentication-to-your-api)

LiveIntent API's usually sit behind the API Gateway. This means that when a request gets to your Laravel API, the request should come with a special, trusted JWT bearer token that identifies the user issuing the request.

To allow your Laravel API to recognize the user from this LI Token, you'll need two things. First, you'll need an authentication guard which verifies that the token is trusted and valid. Second, you'll need a user provider which can turn the LI Token into a workable `User` object that your API can use.

Luckily, both of these are provided for you via the LaravelCommon package.

#### Adding the Token Guard

[](#adding-the-token-guard)

Before you can switch to the LI Token guard, you'll need to register it with your app. You can do this by adding the following line to your application's `AuthServiceProvider`:

```
use LiveIntent\LaravelCommon\LaravelCommon;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    // ...

    LaravelCommon::registerAuthGuard();
}
```

Now that the guard is registered, you can instruct your app to use it by editing your `config/auth.php` file and switching our the default API driver for the `li_token` driver.

```
'api' => [
    'driver' => 'li_token',
    'provider' => 'users',
    'hash' => false,
],
```

However, please note that the default `'users'` provider WILL NOT WORK with the `'li_token'` guard, and you will need to select one of the two below user providers.

#### Adding the User Provider

[](#adding-the-user-provider)

There are two user providers provided with the LaravelCommon package. Of course, you are free to define your own, but these two should cover most cases.

##### Transient User Provider

[](#transient-user-provider)

The `Transient` user provider is meant to be used when you need to authenticate users in your API, but you do not wish to save any user information in your own database. The Transient user provider will provide your app with a workable `User` object that will work will all of Laravel's authentication mechanisms, but it will not persist the User into your database.

To use this provider, you'll need to first register it with your app. You can do this by placing the following line in your app's `AuthServiceProvider`:

```
use LiveIntent\LaravelCommon\LaravelCommon;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    // ...

    LaravelCommon::registerAuthGuard();
    LaravelCommon::registerTransientUserProvider();
}
```

After registering the provider, you'll also need to define a new provider config for it in your `config/auth.php` file under the `'providers'` section. Here you may also define which model should be used as the User model.

```
'providers' => [
    //...

    'li_token' => [
        'driver' => 'li_token_transient',
        'model' => App\Models\User::class,
    ]
```

Finally, we can go back to our previously defined auth guard, and instruct it to use this new `li_token` user provider. Modify the 'provider' of the guard's config like so:

```
'api' => [
    'driver' => 'li_token',
    'provider' => 'li_token,
    'hash' => false,
],
```

That's it! You should now be able to make authenticated requests to your application.

##### Persistent User Provider

[](#persistent-user-provider)

The `Persistent` user provider is a better choice when your app not only needs to authenticate users, but also will be storing some information alongside those users. In this case, the Persistent user provider can retrieve the relevant `User` object from your database, or create a new one if none was found.

To use this provider, you'll need to first register it with your app. You can do this by placing the following line in your app's `AuthServiceProvider`:

```
use LiveIntent\LaravelCommon\LaravelCommon;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    // ...

    LaravelCommon::registerAuthGuard();
    LaravelCommon::registerPersistentUserProvider();
}
```

After registering the provider, you'll also need to define a new provider config for it in your `config/auth.php` file under the `'providers'` section. Here you may also define which model should be used as the User model.

```
'providers' => [
    //...

    'li_token' => [
        'driver' => 'li_token_persistent,
        'model' => App\Models\User::class,
    ]
```

Finally, we can go back to our previously defined auth guard, and instruct it to use this new `li_token` user provider. Modify the 'provider' of the guard's config like so:

```
'api' => [
    'driver' => 'li_token',
    'provider' => 'li_token,
    'hash' => false,
],
```

That's it! You should now be able to make authenticated requests to your application.

###### Customizing the persistance method

[](#customizing-the-persistance-method)

If you require even more granular control over how users will be persisted in your application, you can define a `persistFromTransient` method on your `User` model which will be used in place of the default to persist your user to the database.

```
/**
 * Persist a copy of the transient user in the database.
 */
public function persistFromTransient()
{
    static::upsert(
        [['id' => $this->id]],
        ['id'],
        [],
    );
}
```

The example above makes use of the `upsert` facility of Laravel, but you are free to define the method however you feel makes sense.

### Logging HTTP requests

[](#logging-http-requests)

#### Upgrading from previous implementation

[](#upgrading-from-previous-implementation)

The `AssignRequestId` middleware has now been deprecated and should be removed from your project. Please see the instructions below on how to upgrade.

#### Logging HTTP requests with context

[](#logging-http-requests-with-context)

In your `app/Http/Kernel.php`, update your`$middleware` to include `LogWithRequestContext`.

> **Note:** This middleware *must* be the first one in the array

```
protected $middleware = [
    \LiveIntent\LaravelCommon\Http\Middleware\LogWithRequestContext::class,
    // ... All other middleware
```

You must also make sure you add the `tap` key and value to `stderr` within your `config/logging.php`.

```
'stderr' => [
    // ... Other configs
    'tap' => [
        \LiveIntent\LaravelCommon\Log\HttpLogger::class,
    ],
],
```

> **Note:** If you are using any other type of logging mechanism, add the same `tap` key/value to that as well.

#### Logging HTTP request summary

[](#logging-http-request-summary)

If you wish to add a request summary log entry, then within your app service provider, register the http logger.

```
