1、首先安装,我们需要在项目中运行以下命令安装 laravel 的 Passport 软件包
1
| composer require laravel/passport
|
2、安装成功之后,我们需要设置他们的服务提供者,所以打开 config/app.php 文件,并在providers中添加以下代码
1
| Laravel\Passport\PassportServiceProvider::class,
|
注: laravel5.5
以上版本可以忽略这一步骤,此步骤针对 5.5
以下版本
3、注册之后,迁移数据表,Passport 会生成用于存储客户端和令牌的数据表
接下来我们安装 Passport
以生成令牌和客户端
1
| php artisan passport:install
|
4、完成上面步骤,我们来配置下 Passport
- 打开 模型
app/User.php
我们需要增加 HasApiTokens
- 打开
app/Providers/AuthServiceProvider.php
中增加 Passport::routers()
- 在
config/auth.php
中,更改 api
认证方式为 password
修改 app/User.php
如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <?php
namespace App;
use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; use Laravel\Passport\HasApiTokens;
class User extends Authenticatable { use HasApiTokens, Notifiable;
protected $fillable = [ 'name', 'email', 'password', ];
protected $hidden = [ 'password', 'remember_token', ]; }
|
修改 app/Providers/AuthServiceProvider.php
如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?php
namespace App\Providers;
use Illuminate\Support\Facades\Gate; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Laravel\Passport\Passport; use Laravel\Passport\RouteRegistrar;
class AuthServiceProvider extends ServiceProvider {
protected $policies = [ 'App\Model' => 'App\Policies\ModelPolicy', ];
public function boot() { $this->registerPolicies();
Passport::routes(); } }
|
修改 config/auth.php
如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| return [ ..... ..... 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'passport', 'provider' => 'users', ], ], ..... ..... ]
|
5、配置 routes/api.php 增加相应路由令牌
1 2 3 4 5 6 7 8 9
| Route::post('login', 'API\PassportController@login');
Route::post('register', 'API\PassportController@register');
Route::group(['middleware' => 'auth:api'], function(){ Route::post('get-details', 'API\PassportController@getDetails'); });
|
6、接下来创建控制器
使用命令创建:
1
| php artisan make:controller API/PassportController
|
文件内容修改为如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <?php namespace App\Http\Controllers\API;
use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\User; use Illuminate\Support\Facades\Auth; use Validator;
class PassportController extends Controller {
public $successStatus = 200;
public function login(){ if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){ $user = Auth::user(); $success['token'] = $user->createToken('MyApp')->accessToken; return response()->json(['success' => $success], $this->successStatus); } else{ return response()->json(['error'=>'Unauthorised'], 401); } }
public function register(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required', 'email' => 'required|email', 'password' => 'required', 'c_password' => 'required|same:password', ]);
if ($validator->fails()) { return response()->json(['error'=>$validator->errors()], 401); }
$input = $request->all(); $input['password'] = bcrypt($input['password']); $user = User::create($input); $success['token'] = $user->createToken('MyApp')->accessToken; $success['name'] = $user->name;
return response()->json(['success'=>$success], $this->successStatus); }
public function getDetails() { $user = Auth::user(); return response()->json(['success' => $user], $this->successStatus); } }
|
到此所有代码编写完成,接下来使用 postman
来测试一下使用效果即可