Simple auth package for laravel apps.
Install the package via Composer:
composer require patrikjak/authAfter 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" --forceYou should publish the config file:
php artisan vendor:publish --tag="pjauth-config" --forceor if you want to publish views:
php artisan vendor:publish --tag="pjauth-views" --forceIf 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.
You can choose your custom User model by define AUTH_MODEL in your .env file.
AUTH_MODEL=App\Models\UserBy 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,
],In routes, we use default laravel middleware group web and guest middleware.
Route::middleware(['web', 'guest']);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.
You should publish the migrations:
php artisan vendor:publish --tag="pjauth-migrations"- How to insert default roles?
You can insert default roles by running the following command:
php artisan seed:user-roles --enum=Patrikjak\\Auth\\Models\\RoleTypeEnum 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.
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')),
],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
}