PHPackages                             ryangjchandler/bearer - 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. [API Development](/categories/api)
4. /
5. ryangjchandler/bearer

ActiveLibrary[API Development](/categories/api)

ryangjchandler/bearer
=====================

Minimalistic token-based authentication for Laravel API endpoints.

v2.1.1(2mo ago)8129.8k↓41.3%6MITPHPPHP ^8.2CI passing

Since May 14Pushed 1y ago2 watchersCompare

[ Source](https://github.com/ryangjchandler/bearer)[ Packagist](https://packagist.org/packages/ryangjchandler/bearer)[ Docs](https://github.com/ryangjchandler/bearer)[ GitHub Sponsors](https://github.com/ryangjchandler)[ RSS](/packages/ryangjchandler-bearer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (18)Versions (14)Used By (0)

Bearer
======

[](#bearer)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e91139c8da4726f09251a2ee60ffb29aecc0be8aa185a17f5075a23975416ea0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7279616e676a6368616e646c65722f6265617265722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ryangjchandler/bearer)[![GitHub Tests Action Status](https://camo.githubusercontent.com/bf03642863961d995cfe53946a90e050c2aa58f7df7484a8409b15b5bc1fd184/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7279616e676a6368616e646c65722f6265617265722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/ryangjchandler/bearer/actions?query=workflow%3Arun-tests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/b8fbed97debf1325ff4b2713f849f4a292c8f6cb0fa7693ed5fae5b2081a4ccc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7279616e676a6368616e646c65722f6265617265722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ryangjchandler/bearer)

Minimalistic token-based authorization for Laravel API endpoints.

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

[](#installation)

You can install the package via Composer:

```
composer require ryangjchandler/bearer
```

You can publish and run the migrations with:

```
php artisan vendor:publish --provider="RyanChandler\Bearer\BearerServiceProvider" --tag="bearer-migrations"
php artisan migrate
```

You can publish the config file with:

```
php artisan vendor:publish --provider="RyanChandler\Bearer\BearerServiceProvider" --tag="bearer-config"
```

Usage
-----

[](#usage)

### Creating tokens

[](#creating-tokens)

To create a new token, you can use the `RyanChandler\Bearer\Models\Token` model.

```
use RyanChandler\Bearer\Models\Token;

$token = Token::create([
    'token' => Str::random(32),
]);
```

Alternatively, you can use the `RyanChandler\Bearer\Facades\Bearer` facade to `generate` a token.

```
use RyanChandler\Bearer\Facades\Bearer;

$token = Bearer::generate(domains: [], expiresAt: null, description: null);
```

By default, Bearer uses time-ordered UUIDs for token strings. You can modify this behaviour by passing a `Closure` to `Bearer::generateTokenUsing`. This function must return a string for storage to the database.

```
use RyanChandler\Bearer\Facades\Bearer;

Bearer::generateTokenUsing(static function (): string {
    return (string) Str::orderedUuid();
});
```

### Retrieving a `Token` instance

[](#retrieving-a-token-instance)

To retrieve a `Token` instance from the `token` string, you can use the `RyanChandler\Bearer\Facades\Bearer` facade.

```
use RyanChandler\Bearer\Facades\Bearer;

$token = Bearer::find('my-token-string');
```

### Using a token in a request

[](#using-a-token-in-a-request)

Bearer uses the `Authorization` header of a request to retreive the token instance. You should format it like so:

```
Authorization: Bearer my-token-string

```

### Verifying tokens

[](#verifying-tokens)

To verify a token, add the `RyanChandler\Bearer\Http\Middleware\VerifyBearerToken` middleware to your API route.

```
use RyanChandler\Bearer\Http\Middleware\VerifyBearerToken;

Route::get('/endpoint', MyEndpointController::class)->middleware(VerifyBearerToken::class);
```

### Token expiration

[](#token-expiration)

If you would like a token to expire at a particular time, you can use the `expires_at` column.

```
$token = Bearer::find('my-token-string');

$token->update([
    'expires_at' => now()->addWeek(),
]);
```

Or just use the class's helper methods.

```
$token = Bearer::find('my-token-string');

$token->addWeeks(1)->save();
```

If you try to use the token after this time, it will return an error.

### Limit tokens to a particular domain

[](#limit-tokens-to-a-particular-domain)

Token usage can be restricted to a particular domain. Bearer uses the scheme and host from the request to determine if the token is valid or not.

```
$token = Bearer::find('my-token-string');

$token->update([
    'domains' => [
        'https://laravel.com',
    ],
]);
```

If you attempt to use this token from any domain other than `https://laravel.com`, it will fail and abort.

> **Note**: domain checks include the scheme so be sure to add both cases for HTTP and HTTPS if needed.

### Set a token description

[](#set-a-token-description)

You can optionally set a description for the token.

```
$token = Bearer::find('my-token-string');

$token->update([
    'description' => 'Example description for the token.',
]);
```

> **Note**: The description field accepts a maximum of 255 characters.

Testing
-------

[](#testing)

```
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Ryan Chandler](https://github.com/ryangjchandler)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance63

Regular maintenance activity

Popularity42

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 78.3% 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 ~161 days

Recently: every ~375 days

Total

12

Last Release

60d ago

Major Versions

v0.5.0 → v1.0.02023-04-03

v1.0.0 → v2.0.02024-03-12

PHP version history (4 changes)v0.1.0PHP ^7.4|^8.0

v0.5.0PHP ^8.0

v1.0.0PHP ^8.1

v2.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/568d485d441c691b0358b9091254a6a671fef8f76b73f28af1180ad568d142b2?d=identicon)[ryangjchandler](/maintainers/ryangjchandler)

---

Top Contributors

[![ryangjchandler](https://avatars.githubusercontent.com/u/41837763?v=4)](https://github.com/ryangjchandler "ryangjchandler (47 commits)")[![gregorip02](https://avatars.githubusercontent.com/u/62108989?v=4)](https://github.com/gregorip02 "gregorip02 (6 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (5 commits)")[![Ojsholly](https://avatars.githubusercontent.com/u/29526601?v=4)](https://github.com/Ojsholly "Ojsholly (1 commits)")[![ricardov03](https://avatars.githubusercontent.com/u/2243870?v=4)](https://github.com/ricardov03 "ricardov03 (1 commits)")

---

Tags

apiapi-authlaravellaravel-apilaravelryangjchandlerBearer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ryangjchandler-bearer/health.svg)

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

###  Alternatives

[simplestats-io/laravel-client

Client for SimpleStats!

4515.5k](/packages/simplestats-io-laravel-client)[ryangjchandler/laravel-comments

A dead-simple comments package for Laravel.

22118.2k](/packages/ryangjchandler-laravel-comments)[stechstudio/laravel-hubspot

A Laravel SDK for the HubSpot CRM Api

2971.0k](/packages/stechstudio-laravel-hubspot)[scalar/laravel

Render your OpenAPI-based API reference

6183.9k2](/packages/scalar-laravel)[tapp/filament-webhook-client

Add a Filament resource and a policy for Spatie Webhook client

1120.2k](/packages/tapp-filament-webhook-client)[surface/laravel-webfinger

A Laravel package to create an ActivityPub webfinger.

113.8k](/packages/surface-laravel-webfinger)

PHPackages © 2026

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