如何利用laravel Passport實現API認證

來源:互聯網
上載者:User
這篇文章主要介紹了關於如何使用laravel Passport做API認證,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

安裝larave

laravel new passport_democd passport_demo && composer install

將 .env 中資料庫配置修改為自己的資料庫配置

 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret

安裝Passport

composer require laravel/passportphp artisan migratephp artisan passport:install

執行時可能出現以下錯誤

Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))

編輯

 namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema;//新增 class AppServiceProvider extends ServiceProvider{     public function boot(){}     public function register(){         Schema::defaultStringLength(191);//新增     } }

然後再執行這兩條命令即可

php artisan migratephp artisan passport:install

編輯將添加到

 namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; use Laravel\Passport\HasApiTokens;//新增 class User extends Authenticatable{     use Notifiable;     use HasApiTokens;//新增     protected $fillable = ['name', 'email', 'password',];     protected $hidden = ['password', 'remember_token',]; }

在的方法中調用函數

 namespace App\Providers; use Illuminate\Support\Facades\Gate; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Laravel\Passport\Passport;//新增 class AuthServiceProvider extends ServiceProvider{     protected $policies = ['App\Model' => 'App\Policies\ModelPolicy',];     public function boot(){         $this->registerPolicies();         Passport::routes();//新增     } }

將設定檔中授權看守器的的選項改為

//修改前 'guards' => [     'web' => [         'driver' => 'session',         'provider' => 'users',     ],     'api' => [         'driver' => 'token',         'provider' => 'users',     ], ],
// 修改後 'guards' => [     'web' => [         'driver' => 'session',         'provider' => 'users',     ],     'api' => [         'driver' => 'passport',         'provider' => 'users',     ], ],

建立檔案,這裡的代碼來自另一個教程。

 namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\User; use Illuminate\Support\Facades\Auth; use Validator; class UserController 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 details() {         $user = Auth::user();         return response()->json(['success' => $user], $this->successStatus);     } }

使用postman進行測試
註冊介面,註冊成功後返回token與使用者名稱

登入介面

詳情介面

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.