notion/sway
Composer 安装命令:
composer require notion/sway
包简介
A custom Laravel authentication package with Redis and database fallback for API authentication.
README 文档
README
What is Included:
- Project Introduction: Brief overview of the package.
- Features: Key features of the package listed in bullet points.
- Installation: Step-by-step guide to install and publish the configuration file.
- Usage: How to configure and use the package in your Laravel project.
- Configuration: Details of the
config/sway.phpfile and how to adjust settings. - Contribute: How users can contribute to the project.
- License: Specifies that the package is licensed under the MIT License.
🎉 Notion Sway is a custom Laravel authentication package that provides a flexible JWT-based authentication system with database fallback options for API authentication.
Features
- JWT token-based API authentication: Secure API authentication with JWT tokens.
- Redis and database fallback authentication methods: Provides fallback methods in case the primary authentication method fails.
- Customizable guards: Easily customize authentication guards and configuration.
- Middleware to protect routes: Use middleware to protect routes with custom authentication.
- Easy integration into Laravel: Quick and simple integration into your existing Laravel applications.
- Fully configurable: Configure JWT token expiration time and token prefix.
- Flexible authentication: Supports multiple authentication methods (Redis, Database).
Installation
To install the package, run the following command in your Laravel project:
Installation
$ composer require notion/sway
$ php artisan vendor:publish --provider="Sway\Providers\SwayAuthServiceProvider"
Usage
Configure Authentication Guard
In your config/auth.php file, configure the sway guard by adding it to the guards array:
'guards' => [ 'user:api' => [ 'driver' => 'sway', 'provider' => 'users', // Define the provider for your model ], 'admin:api' => [ 'driver' => 'sway', 'provider' => 'admins', // Define the provider for your model ], ],
Protect Routes with Middleware
In your routes/web.php or routes/api.php file, set authorized middleware then pass the guard to middleware. This is required to get correct auth user while in controller:
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Route; Route::prefix('v1')->middleware(["authorized:" . 'user:api'])->group(function () { // Your routes }); Route::prefix('v1')->middleware(["authorized:" . 'admin:api'])->group(function () { // Your routes });
Generate token
In order to generate token call attempt() method on the guard
- Important: Make sure to pass array and must hold email and password data.
- Output: After success it will return
access_tokenandrefresh_token.
public function login(Request $request) { // Validate the request input $request->validate([ 'email' => 'required|email', 'password' => 'required|string', ]); $credentials = $request->only('email', 'password'); // Use the custom guard to attempt login and generate tokens $accessToken = Auth::guard('user:api')->attempt($credentials); return response()->json([ 'tokens' => $accessToken, 'token_type' => 'bearer', ]); }
Get Authenticated User
To obtain the auth user you can get through Guard or Request:
// Get Authenticated User model Auth::guard('user:api')->user(); // Get Authenticated Admin model Auth::guard('admin:api')->user();
- OR:
// Recomended approach $request->user()
Invalidate Token
- Update Model: use
InvalidatableTokenon your model:
use Sway\Traits\InvalidatableToken; class User extends Authenticatable { use InvalidatableToken; }
- Clear Token: call
invalidateTokento delete token:
public function logout(Request $request) { $user = $request->user(); $deleted = $user->invalidateToken(); // Calls the invalidateToken method defined in the trait return response()->json([ 'success' => $deleted, ]); }
Contributors
notion/sway 适用场景与选型建议
notion/sway 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 67 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 notion/sway 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 notion/sway 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 67
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-21