php - Laravel Passport Multiauth -
i working on api 2 frontend apps (vue2/mobile). 1 uses user
model , other uses admin
model (laravel api)
i using laravel passport
authenticating users , admins, provided access token users i'm facing problem admins
so did
1-> created admin
model
<?php namespace app; use laravel\passport\hasapitokens; use illuminate\notifications\notifiable; use illuminate\foundation\auth\user authenticatable; class admin extends authenticatable { use hasapitokens, notifiable; /** * attributes mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password','role', ]; /** * attributes should hidden arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }
2-> created admins guard uses passport
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'passport', 'provider' => 'users', ], 'admin' => [ 'driver' => 'passport', 'provider' => 'admins', ], ],
3-> created route , controller granting access token admins
route::post('/oauth/token/admin', [ 'uses' => 'auth\customaccesstokencontroller@issueusertoken' ]); <?php namespace app\http\controllers\auth; use illuminate\http\request; use psr\http\message\serverrequestinterface; use laravel\passport\http\controllers\accesstokencontroller; class customaccesstokencontroller extends accesstokencontroller { /** * hooks in before accesstokencontroller issues token * * * @param serverrequestinterface $request * @return mixed */ public function issueusertoken(serverrequestinterface $request) { $httprequest = request(); if ($httprequest->grant_type == 'password') { $admin = \app\admin::where('email', $httprequest->username) ->where('password', $httprequest->password) ->first(); //dd($admin); return $this->issuetoken($request); } } }
4-> tested postman
http://localhost:8000/api/oauth/token/admin client_id:4 client_secret:m4qklqhpkj4pgl52429ripassq3bojktjze3uowk grant_type:password username:admin@gmail.com password:secret //i'm getting { "error": "invalid_credentials", "message": "the user credentials incorrect." } //if use user model credentials username:user@gmail.com password:secret //i'm getting access token { "token_type": "bearer", "expires_in": 31536000, "access_token": "eyj0exaioijkv1qi....", "refresh_token": "ui354efjlvdmoho...." }
i'm tired figuring out went wrong
looking forward needed help
thank you
Comments
Post a Comment