Skip to content

Commit b7fe315

Browse files
authored
更新說明文件 (#4)
* 更新說明文件 * 更新套件資訊 * 移除 composer.lock * 修改 phpunit 設置
1 parent bea21ff commit b7fe315

File tree

8 files changed

+126
-6416
lines changed

8 files changed

+126
-6416
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/vendor
2+
/coverage
23
.env
34
.php_cs.cache
45
.phpunit.result.cache
6+
composer.lock

README.md

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
# A2Workspace/Laravel-JWT
22

3-
一個隨開即用的 API 認證服務。
3+
一個幾乎零配置的 API 認證服務。
44

5-
此套件是基於 [tymon/jwt-auth](https://github.com/tymondesigns/jwt-auth) 的包裝,並提供一個簡易的 `AuthenticatesUsers` 特性方便擴充。
5+
此套件是基於 [php-open-source-saver/jwt-auth](https://github.com/PHP-Open-Source-Saver/jwt-auth/) 的包裝,並提供一個簡易的 `AuthenticatesUsers` 特性方便擴充。
66

7-
此套件相容於 **Nuxt**`auth-nuxt` 模組。如何設定請參考 [# Nuxt 登入](#Nuxt-登入)
7+
特性:
8+
- 支援多使用者模型
9+
- 相容 **Nuxt.js**`auth-nuxt`。如何設定請參考 [# Nuxt 登入](#Nuxt-登入)
810

911

1012
## 安裝
11-
此套件尚未發布到 **Packagist** 需透過下列方法安裝:
13+
執行下列命令透過 **composer** 引入到你的 **Laravel** 專案:
1214

13-
```
14-
composer config repositories.a2workspace/laravel-jwt vcs https://github.com/A2Workspace/laravel-jwt.git
15-
composer require "a2workspace/laravel-jwt:*"
15+
```bash
16+
composer require a2workspace/laravel-jwt
1617
```
1718

18-
接著,你應該執行 `laravel-jwt:install` Artisan 指令來進行安裝。
19-
該指令會生成設定檔與 JWT 密鑰
19+
接著,執行 `laravel-jwt:install` Artisan 指令來進行安裝。
20+
該指令會生成設定檔,並注入 `JWT_SECRET``.env`
2021

21-
```
22+
```bash
2223
php artisan laravel-jwt:install
2324
```
2425

2526
現在應該會有個 `config/jwt.php` 檔案。
2627

28+
-----
2729

2830
## 快速開始
2931

@@ -125,15 +127,63 @@ Route::middleware('auth:api')->get('/auth/user', function (Request $request) {
125127

126128
```
127129

128-
## 自訂認證控制器
130+
-----
129131

130-
`A2Workspace\LaravelJwt\AuthenticatesUsers` 提供了 JWT 登入認證所需的所有方法,僅需要在控制器中使用該特性,並註冊到專案的路由檔案。
132+
## 客製化認證控制器
131133

134+
這裡告訴你如何編寫自己的認證控制器,你可以參考 [A2Workspace/laravel-social-entry-demo](https://github.com/A2Workspace/laravel-social-entry-demo/blob/master/app/Http/Controllers) 中如何配置多使用者模型認證。
135+
136+
一個簡易範例:
132137
```php
133138
<?php
134139

135140
namespace App\Http\Controllers;
136141

142+
use App\Http\Controllers\Controller;
143+
use App\Http\Resources\AdminResource;
144+
use A2Workspace\LaravelJwt\AuthenticatesUsers;
145+
146+
class AuthController extends Controller
147+
{
148+
use AuthenticatesUsers;
149+
150+
/**
151+
* 回傳認證守衛
152+
*
153+
* @return \PHPOpenSourceSaver\JWTAuth\JWTGuard
154+
*/
155+
protected function guard(): JWTGuard
156+
{
157+
return Auth::guard('admin');
158+
}
159+
160+
/**
161+
* 取得驗證使用者名稱的欄位
162+
*
163+
* @return string
164+
*/
165+
protected function username()
166+
{
167+
return 'account';
168+
}
169+
170+
/**
171+
* {@inheritDoc}
172+
*/
173+
public function me(Request $request)
174+
{
175+
return new AdminResource($request->user());
176+
}
177+
}
178+
```
179+
180+
### 引入認證特性到控制器中
181+
182+
`A2Workspace\LaravelJwt\AuthenticatesUsers` 提供了 JWT 登入認證所需的所有方法,僅需要在控制器中使用該特性,並註冊到專案的路由檔案。
183+
184+
```php
185+
namespace App\Http\Controllers;
186+
137187
use App\Http\Controllers\Controller;
138188
use A2Workspace\LaravelJwt\AuthenticatesUsers;
139189

@@ -199,6 +249,8 @@ Route::get('/auth/user', 'AuthController@me');
199249

200250
*注意: 當使用自訂控制器時,就不需要在 `App\Providers\AuthServiceProvider` 中重複註冊 `LaravelJwt:routes` 了。*
201251

252+
-----
253+
202254
## Nuxt 登入
203255

204256
此套件相容於 **Nuxt**`auth-nuxt` 模組中的 `Laravel JWT` ([參考這裡](https://auth.nuxtjs.org/providers/laravel-jwt))。

composer.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "a2workspace/laravel-jwt",
3-
"description": "提供 Laravel JWT 認證功能",
4-
"type": "project",
3+
"description": "一個幾乎零配置的 API 認證服務",
54
"license": "MIT",
65
"authors": [
76
{
@@ -14,7 +13,7 @@
1413
"php-open-source-saver/jwt-auth": "^1.4"
1514
},
1615
"require-dev": {
17-
"orchestra/testbench": "4.x"
16+
"orchestra/testbench": "6.x"
1817
},
1918
"autoload": {
2019
"psr-4": {
@@ -32,5 +31,13 @@
3231
"A2Workspace\\LaravelJwt\\ServiceProvider"
3332
]
3433
}
34+
},
35+
"scripts": {
36+
"test": [
37+
"vendor/bin/phpunit"
38+
],
39+
"test-coverage": [
40+
"vendor/bin/phpunit --coverage-html coverage"
41+
]
3542
}
3643
}

0 commit comments

Comments
 (0)