douxin9135 2017-04-06 10:23
浏览 25

Laravel Passport Multiauth

I am working on an API for 2 frontend apps (vue2/mobile). One uses User model and the other uses the Admin model (Laravel is just an API)

I am using Laravel Passport for authenticating users and admins, i successfully provided access token for users but i'm facing some problem with admins

So for i did

1-> created Admin model

<?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','role',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

2-> created a admins guard which uses passport

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
    'admin' => [
        'driver' => 'passport',
        'provider' => 'admins',
    ],
],

3-> created Route and Controller for granting access token for 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 the AccessTokenController issues a 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-> i tested with 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 were incorrect."
}

//if i use the User model credentials 
username:user@gmail.com
password:secret

//i'm getting the access token 
{
  "token_type": "Bearer",
  "expires_in": 31536000,
  "access_token": "eyJ0eXAiOiJKV1Qi....",
  "refresh_token": "UI354EfJlVdmOhO...."
}

i'm really tired figuring out what went wrong

looking forward for much needed help

thank you

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line