Skip to content

Commit 4f0c2b9

Browse files
committed
編輯說明文件
1 parent 5511415 commit 4f0c2b9

File tree

3 files changed

+250
-2
lines changed

3 files changed

+250
-2
lines changed

README.md

Lines changed: 248 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,248 @@
1-
# A Laravel package example
1+
# A2Workspace/Laravel-JWT
2+
3+
一個隨開即用的 API 認證服務。
4+
5+
此套件是基於 [tymon/jwt-auth](https://github.com/tymondesigns/jwt-auth) 的包裝,並提供一個簡易的 `AuthenticatesUsers` 特性方便擴充。
6+
7+
此套件相容於 **Nuxt**`auth-nuxt` 模組。如何設定請參考 [# Nuxt 登入](#Nuxt-登入)
8+
9+
10+
## 安裝
11+
此套件尚未發布到 **Packagist** 需透過下列方法安裝:
12+
13+
```
14+
composer config repositories.hooks vcs https://github.com/A2Workspace/laravel-jwt.git
15+
composer require "a2workspace/laravel-jwt:*"
16+
```
17+
18+
接著,你應該執行 `laravel-jwt:install` Artisan 指令來進行安裝。
19+
該指令會生成設定檔與 JWT 密鑰。
20+
21+
```
22+
php artisan laravel-jwt:install
23+
```
24+
25+
現在應該會有個 `config/jwt.php` 檔案。
26+
27+
28+
## 快速開始
29+
30+
要讓你的 API 可以透過 *jwt* 登入需要做以下的設定:
31+
32+
33+
### 修改 User 資料模型
34+
35+
首先讓你的 `User` 模型實作 `Tymon\JWTAuth\Contracts\JWTSubject` 介面;
36+
並將 `A2Workspace\LaravelJwt\HasApiTokens` 特性加到你的 `User` 模型中;
37+
38+
```php
39+
<?php
40+
41+
namespace App\Models;
42+
43+
use Illuminate\Database\Eloquent\Factories\HasFactory;
44+
use Illuminate\Foundation\Auth\User as Authenticatable;
45+
use Illuminate\Notifications\Notifiable;
46+
use A2Workspace\LaravelJwt\HasApiTokens;
47+
use Tymon\JWTAuth\Contracts\JWTSubject;
48+
49+
class User extends Authenticatable implements JWTSubject
50+
{
51+
use HasApiTokens, HasFactory, Notifiable;
52+
}
53+
```
54+
有關 `Tymon\JWTAuth\Contracts\JWTSubject` 設定可參考 [https://jwt-auth.readthedocs.io/en/develop/quick-start/#update-your-user-model](https://jwt-auth.readthedocs.io/en/develop/quick-start/#update-your-user-model)
55+
56+
57+
### 設定登入認證守衛 (Auth Guard)
58+
59+
接著,找到你專案的 `config/auth.php` 設定檔。將 `api``driver` 修改為 `jwt`
60+
61+
```php
62+
'guards' => [
63+
'web' => [
64+
'driver' => 'session',
65+
'provider' => 'users',
66+
],
67+
68+
'api' => [
69+
'driver' => 'jwt',
70+
'provider' => 'users',
71+
],
72+
],
73+
```
74+
75+
### 註冊路由
76+
77+
最後,你應該在 `App\Providers\AuthServiceProvider``boot` 中註冊 `LaravelJwt::routes`:
78+
79+
```php
80+
<?php
81+
82+
namespace App\Providers;
83+
84+
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
85+
use Illuminate\Support\Facades\Gate;
86+
use A2Workspace\LaravelJwt\LaravelJwt;
87+
88+
class AuthServiceProvider extends ServiceProvider
89+
{
90+
/**
91+
* The policy mappings for the application.
92+
*
93+
* @var array
94+
*/
95+
protected $policies = [
96+
'App\Models\Model' => 'App\Policies\ModelPolicy',
97+
];
98+
99+
/**
100+
* Register any authentication / authorization services.
101+
*
102+
* @return void
103+
*/
104+
public function boot()
105+
{
106+
$this->registerPolicies();
107+
108+
if (! $this->app->routesAreCached()) {
109+
LaravelJwt::routes();
110+
}
111+
}
112+
}
113+
```
114+
115+
## 自訂使用者回傳值
116+
117+
你可以透過覆寫 `/api/auth/user` 路徑來修改回傳的使用者資訊:
118+
119+
```php
120+
// routes/api.php
121+
122+
Route::middleware('auth:api')->get('/auth/user', function (Request $request) {
123+
return new UserResource($request->user());
124+
});
125+
126+
```
127+
128+
## 自訂認證控制器
129+
130+
`A2Workspace\LaravelJwt\AuthenticatesUsers` 提供了 JWT 登入認證所需的所有方法,僅需要在控制器中使用該特性,並註冊到專案的路由檔案。
131+
132+
```php
133+
<?php
134+
135+
namespace App\Http\Controllers;
136+
137+
use App\Http\Controllers\Controller;
138+
use A2Workspace\LaravelJwt\AuthenticatesUsers;
139+
140+
class AuthController extends Controller
141+
{
142+
use AuthenticatesUsers;
143+
}
144+
```
145+
146+
### 指定認證守衛
147+
148+
需對應 `configs/auth.php` 中的 `guards` 名稱,且 `driver` 必須為 `jwt`
149+
150+
```php
151+
use Tymon\JWTAuth\JWTGuard;
152+
153+
class AuthController extends Controller
154+
{
155+
use AuthenticatesUsers;
156+
157+
/**
158+
* 回傳認證守衛
159+
*
160+
* @return \Tymon\JWTAuth\JWTGuard
161+
*/
162+
protected function guard(): JWTGuard
163+
{
164+
return Auth::guard('custom-api-guard');
165+
}
166+
}
167+
```
168+
169+
### 指定登入時的帳號欄位
170+
171+
預設為 `username`,你可以自行修改為 `phone``email``account`...等。
172+
173+
```php
174+
class AuthController extends Controller
175+
{
176+
use AuthenticatesUsers;
177+
178+
/**
179+
* 取得驗證使用者名稱的欄位
180+
*
181+
* @return string
182+
*/
183+
protected function username()
184+
{
185+
return 'username';
186+
}
187+
}
188+
```
189+
190+
### 註冊路由
191+
192+
```php
193+
// routes/api.php
194+
Route::post('/auth/login', 'AuthController@login');
195+
Route::post('/auth/logout', 'AuthController@logout');
196+
Route::post('/auth/refresh', 'AuthController@refresh');
197+
Route::get('/auth/user', 'AuthController@me');
198+
```
199+
200+
*注意: 當使用自訂控制器時,就不需要在 `App\Providers\AuthServiceProvider` 中重複註冊 `LaravelJwt:routes` 了。*
201+
202+
## Nuxt 登入
203+
204+
此套件相容於 **Nuxt**`auth-nuxt` 模組中的 `Laravel JWT` ([參考這裡](https://auth.nuxtjs.org/providers/laravel-jwt))。
205+
206+
```js
207+
// nuxt.config.js
208+
209+
auth: {
210+
strategies: {
211+
'laravelJWT': {
212+
provider: 'laravel/jwt',
213+
url: '<laravel url>',
214+
endpoints: {
215+
// ...預設這裡不需要修改
216+
},
217+
token: {
218+
property: 'access_token',
219+
maxAge: 60 * 60
220+
},
221+
refreshToken: {
222+
maxAge: 20160 * 60
223+
},
224+
},
225+
}
226+
}
227+
```
228+
229+
當使用 `UserResource` 時,由於使用者資料是放在 `data` 欄位而不是最頂層,需增加 `user.property` 的設定:
230+
231+
```js
232+
// nuxt.config.js
233+
234+
auth: {
235+
strategies: {
236+
'laravelJWT': {
237+
provider: 'laravel/jwt',
238+
239+
// ...
240+
241+
user: {
242+
property: 'data'
243+
},
244+
245+
},
246+
}
247+
}
248+
```

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "a2workspace/laravel-jwt",
33
"description": "提供 Laravel JWT 認證功能",
4+
"type": "project",
45
"license": "MIT",
56
"authors": [
67
{

src/AuthenticatesUsers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function credentials(Request $request)
3838
}
3939

4040
/**
41-
* 回傳驗證守衛。
41+
* 回傳認證守衛
4242
*
4343
* @return \Tymon\JWTAuth\JWTGuard
4444
*/

0 commit comments

Comments
 (0)