patrikjak / auth
Simple auth package for laravel apps
Installs: 250
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/patrikjak/auth
Requires
- php: ^8.4
- laravel/framework: ^12.1.1
- laravel/socialite: ^5.16
- patrikjak/utils: ^2.3.0
Requires (Dev)
- larastan/larastan: ^3.0
- orchestra/testbench: ^10.0.0
- phpstan/phpstan: ^2.1.0
- phpunit/phpunit: ^11.0
- slevomat/coding-standard: ~8.0
- spatie/phpunit-snapshot-assertions: ^5.1
README
Simple auth package for laravel apps.
Installation
Install the package via Composer:
composer require patrikjak/auth
Setup
After installing the package, add the package provider to the providers array in bootstrap/providers.php.
use Patrikjak\Auth\AuthServiceProvider; use Patrikjak\Utils\UtilsServiceProvider; return [ ... UtilsServiceProvider::class, AuthServiceProvider::class, ];
You need to have installed and configured patrikjak/utils package.
After that you need to publish the package assets (if you configured patrikjak/utils package, you don't need to publish assets again):
php artisan vendor:publish --tag="pjauth-assets" --force
You should publish the config file:
php artisan vendor:publish --tag="pjauth-config" --force
or if you want to publish views:
php artisan vendor:publish --tag="pjauth-views" --force
If you don't publish config file, you will miss all features of this package. I recommend add this script to your composer.json file:
"scripts": { "post-update-cmd": [ "@php artisan vendor:publish --tag=pjauth-config --force", ] }
It will publish config file every time you update your composer packages.
Laravel cannot merge multidimensional arrays in config files.
General
You can choose your custom User model by define AUTH_MODEL in your .env file.
AUTH_MODEL=App\Models\User
By default Patrikjak\Auth\Models\User model is used.
Also you can change the default user repository implementation. You need to change in config/pjauth.php file.
'repositories' => [ 'user' => \Patrikjak\Auth\Repositories\UserRepository::class, ],
Routes
In routes, we use default laravel middleware group web and guest middleware.
Route::middleware(['web', 'guest']);
Middlewares
There is prepared middleware for checking user roles. You can use it in your routes.
use Patrikjak\Auth\Http\Middlewares\VerifyRole; use Patrikjak\Auth\Models\RoleType; Route::middleware(['web', 'auth', VerifyRole::withRole(RoleType::ADMIN)]);
It will check role of the user and if it is not the same as the role in the middleware, it will return 403 status code. Super admin has all roles.
Migrations
You should publish the migrations:
php artisan vendor:publish --tag="pjauth-migrations"
Roles
- How to insert default roles?
You can insert default roles by running the following command:
php artisan seed:user-roles --enum=Patrikjak\\Auth\\Models\\RoleType
Enum is default Patrikjak\Auth\Models\RoleType enum class. You can create your own enum class and pass it as an argument.
It must use Patrikjak\Utils\Common\Traits\EnumValues trait.
Socialite
You need to add your socialite credentials to your .env file.
GOOGLE_CLIENT_ID= GOOGLE_CLIENT_SECRET=
And add the following to your config/services.php file:
'google' => [ 'client_id' => env('GOOGLE_CLIENT_ID'), 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 'redirect' => sprintf('%s/auth/google/callback', env('APP_URL')), ],
Change password
If you want to change the password, you need to allow it in the config/pjauth.php file.
'features' => [ 'change_password' => true, ],
After that, you can use the following route:
route('api.change-password');
By default, it validates old password. If you want to turn off old password validation, you need to send it in the request.
{
"old_password": "old_password",
"password": "new_password",
"password_confirmation": "new_password",
"validate_old_password": false
}