{"id":16675813,"url":"https://github.com/mpyw/scoped-auth","last_synced_at":"2025-03-21T18:31:35.279Z","repository":{"id":39420074,"uuid":"227085809","full_name":"mpyw/scoped-auth","owner":"mpyw","description":"Apply specific scope for user authentication.","archived":false,"fork":false,"pushed_at":"2025-03-05T10:55:00.000Z","size":40,"stargazers_count":10,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-15T01:51:50.454Z","etag":null,"topics":["auth","illuminate","laravel","scope"],"latest_commit_sha":null,"homepage":"","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.md","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-10T10:05:00.000Z","updated_at":"2025-03-05T10:55:04.000Z","dependencies_parsed_at":"2024-10-28T11:28:20.861Z","dependency_job_id":"0e7d8fd2-f365-4c73-b08b-1181ae1ca7a9","html_url":"https://github.com/mpyw/scoped-auth","commit_stats":{"total_commits":34,"total_committers":4,"mean_commits":8.5,"dds":0.3529411764705882,"last_synced_commit":"25ef1ebd8ddd409c63407225ef92ba8b67c5f04f"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Fscoped-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Fscoped-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Fscoped-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpyw%2Fscoped-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpyw","download_url":"https://codeload.github.com/mpyw/scoped-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244848582,"owners_count":20520549,"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","scope"],"created_at":"2024-10-12T13:07:57.979Z","updated_at":"2025-03-21T18:31:35.224Z","avatar_url":"https://github.com/mpyw.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Scoped Auth\n\n[![Build Status](https://github.com/mpyw/scoped-auth/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/mpyw/scoped-auth/actions)\n[![Coverage Status](https://coveralls.io/repos/github/mpyw/scoped-auth/badge.svg?branch=master)](https://coveralls.io/github/mpyw/scoped-auth?branch=master)\n\nApply specific scope for user authentication.\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\nVia [Composer](https://getcomposer.org/)\n\n```bash\n$ composer require mpyw/scoped-auth\n```\n\n### For [Fortify](https://github.com/laravel/fortify) users\n\n\u003e [!WARNING]\n\u003e Default Fortify's [`RedirectIfTwoFactorAuthenticatable`](https://github.com/laravel/fortify/blob/7da6504f5f8a5fe6854dedaffc81ac497194ba56/src/Actions/RedirectIfTwoFactorAuthenticatable.php#L89-L91) implementation directly uses internal `Model` under `UserProvider`, however, [the Laravel author won't be willing to fix it for whatever reason](https://github.com/laravel/fortify/pull/393). So we need to configure Fortify like this:\n\n\u003cdetails\u003e\n\u003csummary\u003eCustomFortifyAuthenticator.php\u003c/summary\u003e\n\n```php\n\u003c?php\n\nnamespace App\\Auth;\n\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Contracts\\Auth\\Authenticatable;\nuse Illuminate\\Contracts\\Auth\\StatefulGuard;\nuse Illuminate\\Contracts\\Auth\\UserProvider;\nuse Laravel\\Fortify\\Fortify;\n\nclass CustomFortifyAuthenticator\n{\n    private const PASSWORD_NAME = 'password';\n\n    private readonly UserProvider $provider;\n\n    public function __construct(StatefulGuard $guard)\n    {\n        // Assert `StatefulGuard` has `getProvider()` which is not declared in the contract\n        assert(method_exists($guard, 'getProvider'));\n        $provider = $guard-\u003egetProvider();\n\n        assert($provider instanceof UserProvider);\n        $this-\u003eprovider = $provider;\n    }\n\n    public function __invoke(Request $request): ?Authenticatable\n    {\n        $user = $this-\u003eprovider-\u003eretrieveByCredentials([\n            Fortify::username() =\u003e $request-\u003einput(Fortify::username()),\n        ]);\n\n        return $user \u0026\u0026 $this-\u003eprovider-\u003evalidateCredentials($user, [\n            self::PASSWORD_NAME =\u003e $request-\u003einput(self::PASSWORD_NAME),\n        ]) ? $user : null;\n    }\n}\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eAuthServiceProvider.php\u003c/summary\u003e\n\n```php\n\u003c?php\n\nnamespace App\\Providers;\n\nuse App\\Auth\\CustomFortifyAuthenticator;\nuse Illuminate\\Support\\ServiceProvider;\nuse Laravel\\Fortify\\Fortify;\n\nclass AuthServiceProvider extends ServiceProvider\n{\n    public function boot(CustomFortifyAuthenticator $authenticator): void\n    {\n        Fortify::authenticateUsing($authenticator);\n    }\n}\n```\n\u003c/details\u003e\n\n\u003e [!NOTE]\n\u003e Re-submitted PR [laravel/fortify#582](https://github.com/laravel/fortify/pull/582) has been already merged, but it's not released yet at 2025-03-05. Once it's released, you can remove this workaround.\n\n## Testing\n\nVia [PHPUnit](https://phpunit.de/)\n\n```bash\n$ composer test\n```\n\n## Usage\n\nImplement **AuthScopable** contract on your Authenticatable Eloquent Model.\n\n```php\n\u003c?php\n\nnamespace App;\n\nuse Illuminate\\Auth\\Authenticatable;\nuse Illuminate\\Contracts\\Auth\\Authenticatable as UserContract;\nuse Illuminate\\Database\\Eloquent\\Builder;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Mpyw\\ScopedAuth\\AuthScopable;\n\nclass User extends Model implements UserContract, AuthScopable\n{\n    use Authenticatable;\n\n    public function scopeForAuthentication(Builder $query): Builder\n    {\n        return $query-\u003ewhere('active', 1);\n    }\n}\n```\n\n```php\n\u003c?php\n\nuse Illuminate\\Support\\Facades\\Auth;\n\n$user = Auth::user(); // Only include users where \"active\" is 1\n```\n\nNote that you can reuse another existing scope.\n\n```php\npublic function scopeActive(Builder $query): Builder\n{\n    return $query-\u003ewhere('active', 1);\n}\n\npublic function scopeForAuthentication(Builder $query): Builder\n{\n    return $this-\u003escopeActive($query);\n}\n```\n\nAs a by-product, you can also run scope queries based on the standard Eloquent way.\n\n```php\n$user = User::where('email', 'xxx@example.com')-\u003eforAuthentication()-\u003efirstOrFail();\n```\n\n```php\n$user = User::where('email', 'xxx@example.com')-\u003escopes(['forAuthentication'])-\u003efirstOrFail();\n```\n\n## Standards\n\n- [PSR-1: Basic Coding Standard](https://www.php-fig.org/psr/psr-1/)\n- [PSR-2: Coding Style Guide](https://www.php-fig.org/psr/psr-2/)\n- [PSR-4: Autoloader](https://www.php-fig.org/psr/psr-4/)\n- [Semantic Versioning 2.0.0](https://semver.org/)\n\n## Credits\n\n- [mpyw](https://github.com/mpyw)\n- [All Contributors](../../contributors)\n\n## License\n\nLicensed under the MIT License. See [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpyw%2Fscoped-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpyw%2Fscoped-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpyw%2Fscoped-auth/lists"}