A simple and configurable Laravel package for easy API authentication with standardized responses.
danilowa/laravel-api-auth is a Laravel package for a simple and configurable laravel package for easy api authentication with standardized responses..
It currently has 1 GitHub stars and 19 downloads on Packagist (latest version 1.0.3).
Install it with composer require danilowa/laravel-api-auth.
Discover more Laravel packages by danilowa
or browse all Laravel packages to compare alternatives.
Last updated
Streamline API Authentication with Laravel API Auth! This simple and configurable package provides an easy solution for managing API authentication in Laravel applications using Laravel Sanctum. It includes features for user registration, login, logout, and retrieving current user data, all with standardized JSON responses.
Ensure your project meets the following requirements before using this package:
To integrate the Laravel API Auth into your Laravel project, follow these steps:
Run the following command in your terminal:
composer require danilowa/laravel-api-auth
After installation, publish the configuration file:
php artisan vendor:publish --provider="Danilowa\LaravelApiAuth\Providers\ApiAuthServiceProvider"
This will create a configuration file at config/apiauth.php, where you can customize the package settings.
The package configuration can be found in the config/apiauth.php file. This file allows you to customize various aspects of the API authentication system according to your project's needs.
Route Prefix:
route_prefix: Allows you to define a custom prefix for all authentication routes. By default, it's set to auth. You can customize this via the API_AUTH_ROUTE_PREFIX environment variable, making it easy to change to v1/auth, for example.User Model Configuration:
user_model: Specify the class name of the user model that will be used for authentication. The default is App\Models\User::class. If you have a custom user model, change this value accordingly.Token Settings:
default_token_name: Defines the default name for the access tokens generated during registration or login. You can use a different name for each token if desired.Token Revocation Strategy:
revoke_all_tokens: A boolean value that determines whether all tokens for a user should be revoked upon logout. If set to true, all tokens will be revoked; if false, only the current token will be revoked.Customizable Messages:
messages: Allows you to customize the messages returned during the authentication process. For example, you can modify messages like "User created successfully!" to fit your communication style.Validation Rules:
validation: Defines the validation rules for login and registration requests. You can adjust these rules to meet your application's policies, including format requirements for email or password strength.return [
'route_prefix' => env('API_AUTH_ROUTE_PREFIX', 'auth'),
'user_model' => 'App\Models\User::class',
'default_token_name' => 'default_token',
'revoke_all_tokens' => true,
'messages' => [
'user_created' => 'User created successfully!',
'user_logged_in' => 'User logged in!',
'credentials_incorrect' => 'The provided credentials are incorrect.',
'tokens_revoked' => 'Tokens revoked successfully!',
'default_error' => 'An error occurred.',
],
'validation' => [
'login' => [
'rules' => [
'email' => 'required|email',
'password' => 'required|string',
],
],
'registration' => [
'rules' => [
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users',
'password' => 'required|string|min:8',
'token_name' => 'nullable|string',
],
],
],
];
This package provides the following API endpoints for user authentication:
To register a new user, send a POST request to /auth/register with the following JSON body:
{
"name": "John Doe",
"email": "[email protected]",
"password": "password123"
}
To log in, send a POST request to /auth/login:
{
"email": "[email protected]",
"password": "password123"
}
To log out the authenticated user, send a POST request to /auth/logout.
To retrieve the current user's data, send a GET request to /auth/user with the appropriate authentication token.
POST /auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "[email protected]",
"password": "password123"
}
{
"status": "success",
"message": "User registered successfully.",
"data": {
"user": {
"id": 1,
"name": "John Doe",
"email": "[email protected]"
}
}
}
POST /auth/login
Content-Type: application/json
{
"email": "[email protected]",
"password": "password123"
}
{
"status": "success",
"message": "User logged in successfully.",
"data": {
"token": "your_jwt_token_here"
}
}
POST /auth/logout
Authorization: Bearer your_jwt_token_here
{
"status": "success",
"message": "User logged out successfully."
}
GET /auth/user
Authorization: Bearer your_jwt_token_here
{
"status": "success",
"message": "User data retrieved successfully.",
"data": {
"id": 1,
"name": "John Doe",
"email": "[email protected]"
}
}
You can contribute by forking the repository and submitting a pull request.
This package is licensed under the MIT License.
For any questions or feedback, please reach out to: