接下来只需按照下面的步骤,我们就能很快速地将Passport集成到您的laravel项目中
composer require laravel/passport
'providers' => [
'''
Laravel\Passport\PassprotServiceProvider::class,
],
注:laravel5.5
以上版本可以忽略这一步骤,此步骤争对5.5
以下版本
php artisan migrate
接下来我们安装 Passport
以生成令牌和客户端
php artisan passport:install
app/User.php
我们需要增加 HasApiTokens
app/Providers/AuthServiceProvider.php
中增加 Passport::routers()
config/auth.php
中,更改 api
认证方式为 password
<?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;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
<?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
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
}
return [
.....
.....
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
.....
.....
]
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');
});
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;
/**
* login api
*
* @return \Illuminate\Http\Response
*/
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);
}
}
/**
* Register api
*
* @return \Illuminate\Http\Response
*/
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);
}
/**
* details api
*
* @return \Illuminate\Http\Response
*/
public function getDetails()
{
$user = Auth::user();
return response()->json(['success' => $user], $this->successStatus);
}
}
到此所有代码编写完成,接下来我们来测试使用它
php artisan serv
1、我们使用 postman
工具来测试 register api
[](https://lccdn.phphub.org/uploads/images/201804/20/14139/i8em8VMvJh.png?imageView2/2/w/1240/h/0)
[
](https://lccdn.phphub.org/uploads/images/201804/20/14139/i8em8VMvJh.png?imageView2/2/w/1240/h/0)
2、测试 login api
使用第一步注册的邮箱和密码登录
[](https://lccdn.phphub.org/uploads/images/201804/20/14139/YrIkiRb2qr.png?imageView2/2/w/1240/h/0)
[
](https://lccdn.phphub.org/uploads/images/201804/20/14139/YrIkiRb2qr.png?imageView2/2/w/1240/h/0)
3、使用第二步获取到的 token
值来获取用户信息
[](https://lccdn.phphub.org/uploads/images/201804/20/14139/XGjjZhc6pu.png?imageView2/2/w/1240/h/0)
[
](https://lccdn.phphub.org/uploads/images/201804/20/14139/XGjjZhc6pu.png?imageView2/2/w/1240/h/0)
必须把Token值放到header头部信息里面才行。到此大功告成,需要的朋友可以拿走~
Original url: Access
Created at: 2018-10-10 17:07:27
Category: default
Tags: none
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论