{"id":16675825,"url":"https://github.com/mpyw/null-auth","last_synced_at":"2025-12-30T13:23:50.868Z","repository":{"id":49158546,"uuid":"226939681","full_name":"mpyw/null-auth","owner":"mpyw","description":"Null Guard for Laravel. Designed for Middleware-based authentication and testing.","archived":false,"fork":false,"pushed_at":"2025-03-04T06:54:12.000Z","size":59,"stargazers_count":18,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T01:13:59.006Z","etag":null,"topics":["auth","illuminate","laravel","nothing","null"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mpyw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-09T18:27:55.000Z","updated_at":"2025-03-04T06:54:15.000Z","dependencies_parsed_at":"2024-03-13T04:47:49.237Z","dependency_job_id":null,"html_url":"https://github.com/mpyw/null-auth","commit_stats":{"total_commits":20,"total_committers":3,"mean_commits":6.666666666666667,"dds":0.09999999999999998,"last_synced_commit":"0a36dd02b2a7eadf6637db97b694cb871d5a63cd"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Fnull-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Fnull-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Fnull-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Fnull-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpyw","download_url":"https://codeload.github.com/mpyw/null-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137891,"owners_count":21053775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","illuminate","laravel","nothing","null"],"created_at":"2024-10-12T13:08:03.442Z","updated_at":"2025-12-30T13:23:50.798Z","avatar_url":"https://github.com/mpyw.png","language":"PHP","readme":"# Null Auth [![Build Status](https://github.com/mpyw/null-auth/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/mpyw/null-auth/actions) [![Coverage Status](https://coveralls.io/repos/github/mpyw/null-auth/badge.svg?branch=master)](https://coveralls.io/github/mpyw/null-auth?branch=master)\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://user-images.githubusercontent.com/1351893/70479968-e64e8e00-1b21-11ea-83c6-1d5e51f7e3e5.png\"\u003e\n\u003c/p\u003e\n\nNull Guard for Laravel. Designed for Middleware-based authentication and testing.\n\n## Requirements\n\n- PHP: `^8.2`\n- Laravel: `^11.0 || ^12.0`\n\n\u003e [!NOTE]\n\u003e Older versions have outdated dependency requirements. If you cannot prepare the latest environment, please refer to past releases.\n\n## Installing\n\n```bash\ncomposer require mpyw/null-auth\n```\n\n## Features\n\n### `NullAuthenticatable` family\n\n| Trait | ID | Password | Remember Token |\n|:---|:---:|:---:|:---:|\n| `GenericNullAuthenticatable`\u003cbr\u003e`GenericStrictNullAuthenticatable` | ❗️ | ❌| ❌|\n| `NullAuthenticatable`\u003cbr\u003e`StrictNullAuthenticatable` | ✅| ❌| ❌|\n| `NoRememberTokenAuthenticatable`\u003cbr\u003e`StrictNoRememberTokenAuthenticatable` | ✅| ✅| ❌|\n\n- ❗️shows containing abstract methods.\n- `Strict` traits throw `BadMethodCallException` on bad method calls.\n\n### `NullGuard`\n\n- `NullGuard::user()` always returns Authenticatable already set by `NullGuard::setUser()`.\n- `NullGuard::unsetUser()` can unset user.\n\n### `NullUserProvider`\n\n- All methods do nothing and always returns falsy value.\n\n## Usage\n\n### Basic Usage\n\nEdit your **`config/auth.php`**.\n\n```php\n\u003c?php\n\nreturn [\n\n    /* ... */\n\n    /*\n    |--------------------------------------------------------------------------\n    | Authentication Guards\n    |--------------------------------------------------------------------------\n    |\n    | Next, you may define every authentication guard for your application.\n    | Of course, a great default configuration has been defined for you\n    | here which uses session storage and the Eloquent user provider.\n    |\n    | All authentication drivers have a user provider. This defines how the\n    | users are actually retrieved out of your database or other storage\n    | mechanisms used by this application to persist your user's data.\n    |\n    | Supported: \"session\", \"token\"\n    |\n    */\n\n    'guards' =\u003e [\n        'web' =\u003e [\n            'driver' =\u003e 'null', // Use NullGuard for \"web\"\n            'provider' =\u003e 'users',\n        ],\n\n        'api' =\u003e [\n            'driver' =\u003e 'token',\n            'provider' =\u003e 'users',\n            'hash' =\u003e false,\n        ],\n    ],\n\n    /*\n    |--------------------------------------------------------------------------\n    | User Providers\n    |--------------------------------------------------------------------------\n    |\n    | All authentication drivers have a user provider. This defines how the\n    | users are actually retrieved out of your database or other storage\n    | mechanisms used by this application to persist your user's data.\n    |\n    | If you have multiple user tables or models you may configure multiple\n    | sources which represent each model / table. These sources may then\n    | be assigned to any extra authentication guards you have defined.\n    |\n    | Supported: \"database\", \"eloquent\"\n    |\n    */\n\n    'providers' =\u003e [\n        'users' =\u003e [\n            'driver' =\u003e 'null', // Use NullUserProvider for \"users\"\n        ],\n        \n        // 'users' =\u003e [\n        //     'driver' =\u003e 'eloquent',\n        //     'model' =\u003e App\\User::class,\n        // ],\n\n        // 'users' =\u003e [\n        //     'driver' =\u003e 'database',\n        //     'table' =\u003e 'users',\n        // ],\n    ],\n\n    /* ... */\n];\n```\n\n## Motivation\n\n### Guard-based API Authentication\n\nConsider authentication that sends HTTP requests to the external platform.\n\nIn such a situation, you will probably use [`RequestGuard`] through [`Auth::viaRequest()`] call.\nHowever, some methods on the contracts [`UserProvider`] and [`Authenticatable`] are unsuitable for that.\nThey heavily rely on the following flow:\n\n1. Retrieve a user from database by email address\n2. Verify user's password hash\n\nThis library provides a helper that makes the useless contract methods to do nothing; always return nullish or falsy values.\n\nNow we include **`NullAuthenticatable`** trait on a user model.\n\n```php\n\u003c?php\n\nnamespace App;\n\nuse Illuminate\\Contracts\\Auth\\Authenticatable;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Mpyw\\NullAuth\\NullAuthenticatable;\n\nclass User extends Model implements Authenticatable\n{\n    use NullAuthenticatable;\n}\n```\n\nThen only **[`getAuthIdentifierName()`]** and **[`getAuthIdentifier()`]** are provided as a valid implementation.\n\n```php\n\u003c?php\n\n$user = User::find(1);\n\n// Minimal implementation for Authenticatable\nvar_dump($user-\u003egetAuthIdentifierName()); // string(2) \"id\"\nvar_dump($user-\u003egetAuthIdentifier());     // int(1)\n\n// Useless implementation for Authenticatable when we don't use StatefulGuard\nvar_dump($user-\u003egetAuthPassword());       // string(0) \"\"\nvar_dump($user-\u003egetRememberTokenName());  // string(0) \"\"\nvar_dump($user-\u003egetRememberToken());      // string(0) \"\"\n$user-\u003esetRememberToken('...');           // Does nothing\n```\n\n### Middleware-based Authentication\n\nSuppose you hate using [`RequestGuard`] and wish implementing authentication on middleware.\n\nMethods such as [`Auth::user()`] cause side effects when called for the first time on each request.\nThis may lead to inappropriate behaviors if you concentrate authentication on middleware\nand use [`Auth::user()`] only as a container for [`Authenticatable`] object.\n\nDon't worry. This library provides **`NullGuard`**,\nwhich is exactly as a simple [`Authenticatable`] container that you want.\n[`Auth::user()`] does nothing but returning the cached [`Authenticatable`].\nYou can just focus on calling [`Auth::setUser()`] on your cool middleware at ease.\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Middleware;\n\nuse App\\User;\nuse Closure;\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Support\\Facades\\Auth;\nuse Example\\API\\Authentication\\Client;\n\nclass AuthenticateThroughExternalAPI\n{\n    /**\n     * @var \\Example\\API\\Authentication\\Client\n     */\n    protected $client;\n\n    /**\n     * @param \\Example\\API\\Authentication\\Client $client\n     */\n    public function __construct(Client $client)\n    {\n        $this-\u003eclient = $client;\n    }\n\n    /**\n     * @param \\Illuminate\\Http\\Request $request\n     * @param \\Closure $next\n     * @return mixed\n     */\n    public function handle(Request $request, Closure $next)\n    {\n        // Return user_id on success, throw AuthenticationException on failure\n        $userId = $this-\u003eclient-\u003eauthenticate($request-\u003einput('token'));\n\n        // Return User on success, throw ModelNotFoundException on failure\n        $user = User::findOrFail($userId);\n\n        Auth::setUser($user);\n\n        return $next($request);\n    }\n}\n\n```\n\n### Testing\n\nNeedless to say, it is also useful for testing.\nThere is no worry about causing side effects.\n\n[`RequestGuard`]: https://github.com/illuminate/auth/blob/master/RequestGuard.php\n[`Auth::viaRequest()`]: https://github.com/illuminate/auth/blob/7b2297b6cd5e7000b31caca40399c33832237649/AuthManager.php#L219-L235\n[`UserProvider`]: https://github.com/illuminate/contracts/blob/master/Auth/UserProvider.php\n[`Authenticatable`]: https://github.com/illuminate/contracts/blob/master/Auth/Authenticatable.php\n[`getAuthIdentifierName()`]: https://github.com/illuminate/contracts/blob/6b0122dc740d6db5ab8b1187af313c6e65afeb55/Auth/Authenticatable.php#L7-L12\n[`getAuthIdentifier()`]: https://github.com/illuminate/contracts/blob/6b0122dc740d6db5ab8b1187af313c6e65afeb55/Auth/Authenticatable.php#L14-L19\n[`Auth::user()`]: https://github.com/illuminate/contracts/blob/6b0122dc740d6db5ab8b1187af313c6e65afeb55/Auth/Guard.php#L21-L26\n[`Auth::setUser()`]: https://github.com/illuminate/contracts/blob/6b0122dc740d6db5ab8b1187af313c6e65afeb55/Auth/Guard.php#L43-L49\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpyw%2Fnull-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpyw%2Fnull-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpyw%2Fnull-auth/lists"}