danilowa/laravel-api-auth
Composer 安装命令:
composer require danilowa/laravel-api-auth
包简介
A simple and configurable Laravel package for easy API authentication with standardized responses.
关键字:
README 文档
README
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.
Index
- Features
- Requirements
- Installation
- Configuration
- API Endpoints
- Usage Examples
- Documentation
- Contributing
- License
- Contact
Features
- Seamless User Authentication: Easily manage user authentication using Laravel Sanctum.
- Standardized JSON Responses: Consistent response structure for all API interactions, simplifying error handling and data management.
- Flexible Configuration: Customize response formats and API settings according to your application's needs.
Requirements
Ensure your project meets the following requirements before using this package:
- Laravel Framework: Version 9.0 or higher.
- PHP: Version 8.0 or higher.
- Composer: PHP dependency manager.
Installation
To integrate the Laravel API Auth into your Laravel project, follow these steps:
- Install via Composer:
Run the following command in your terminal:
composer require danilowa/laravel-api-auth
- Publish the Configuration (Optional):
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.
Configuration
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.
Key Configurations:
-
Route Prefix:
route_prefix: Allows you to define a custom prefix for all authentication routes. By default, it's set toauth. You can customize this via theAPI_AUTH_ROUTE_PREFIXenvironment variable, making it easy to change tov1/auth, for example.
-
User Model Configuration:
user_model: Specify the class name of the user model that will be used for authentication. The default isApp\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 totrue, all tokens will be revoked; iffalse, 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.
Configuration Example
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', ], ], ], ];
API Endpoints
This package provides the following API endpoints for user authentication:
- POST /auth/register: Register a new user.
- POST /auth/login: Log in an existing user.
- POST /auth/logout: Log out the authenticated user.
- GET /auth/user: Retrieve the current authenticated user's information.
Usage Examples
Register User
To register a new user, send a POST request to /auth/register with the following JSON body:
{
"name": "John Doe",
"email": "john.doe@example.com",
"password": "password123"
}
Login User
To log in, send a POST request to /auth/login:
{
"email": "john.doe@example.com",
"password": "password123"
}
Logout User
To log out the authenticated user, send a POST request to /auth/logout.
Retrieve Current User
To retrieve the current user's data, send a GET request to /auth/user with the appropriate authentication token.
Documentation
API Endpoints
Register
- Description: This endpoint allows a new user to register for the application by providing their name, email, and password.
- Example Request:
POST /auth/register Content-Type: application/json { "name": "John Doe", "email": "john.doe@example.com", "password": "password123" }
- Example Response:
{
"status": "success",
"message": "User registered successfully.",
"data": {
"user": {
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com"
}
}
}
Login
- Description: This endpoint allows an existing user to log in by providing their email and password.
- Example Request:
POST /auth/login Content-Type: application/json { "email": "john.doe@example.com", "password": "password123" }
- Example Response:
{
"status": "success",
"message": "User logged in successfully.",
"data": {
"token": "your_jwt_token_here"
}
}
Logout
- Description: This endpoint allows the authenticated user to log out of the application.
- Example Request:
POST /auth/logout Authorization: Bearer your_jwt_token_here
- Example Response:
{
"status": "success",
"message": "User logged out successfully."
}
Current User
- Description: This endpoint retrieves the current authenticated user's information.
- Example Request:
GET /auth/user Authorization: Bearer your_jwt_token_here
- Example Response:
{
"status": "success",
"message": "User data retrieved successfully.",
"data": {
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com"
}
}
Contributing
You can contribute by forking the repository and submitting a pull request.
License
This package is licensed under the MIT License.
Contact
For any questions or feedback, please reach out to:
- Danilo Oliveira: daniloworkdev@gmail.com
- Website: daniloo.dev
danilowa/laravel-api-auth 适用场景与选型建议
danilowa/laravel-api-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「api」 「Authentication」 「auth」 「laravel」 「laravel-auth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 danilowa/laravel-api-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 danilowa/laravel-api-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 danilowa/laravel-api-auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
A PSR-7 compatible library for making CRUD API endpoints
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-23