10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

laravel/passportのrouteのprefixをデフォルトのoauth/から任意のルートに変更する【Laravel5.5】

Last updated at Posted at 2018-03-11

概要

LaravelでAPI認証する際に利用するライブラリ、laravel/passportのrouteをデフォルトの oauth/ から変更する方法。

※当記事ではlaravel/passportの導入方法は触れてれていないので、基本的な使い方、導入方法はこちらの記事を参照

Laravel5.5でAPI認証のパッケージ(Laravel Passport)を利用する - Qiita
https://qiita.com/niiyz/items/fffff94acb6061ecc9d4

前提

通常、laravel/passportをinstallして各種設定をしたら、以下のようなrouteが生成される。

# php artisan route:list
|        | POST      | oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\ApproveAuthorizationController@approve  | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\AuthorizationController@authorize       | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\DenyAuthorizationController@deny        | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | oauth/clients                           |                 | Laravel\Passport\Http\Controllers\ClientController@store                  | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | oauth/clients                           |                 | Laravel\Passport\Http\Controllers\ClientController@forUser                | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | oauth/clients/{client_id}               |                 | Laravel\Passport\Http\Controllers\ClientController@destroy                | Barryvdh\Cors\HandleCors::class,web,auth |
|        | PUT       | oauth/clients/{client_id}               |                 | Laravel\Passport\Http\Controllers\ClientController@update                 | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | oauth/personal-access-tokens            |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@forUser   | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | oauth/personal-access-tokens            |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@store     | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | oauth/personal-access-tokens/{token_id} |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@destroy   | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | oauth/scopes                            |                 | Laravel\Passport\Http\Controllers\ScopeController@all                     | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | oauth/token                             |                 | Laravel\Passport\Http\Controllers\AccessTokenController@issueToken        | Barryvdh\Cors\HandleCors::class,throttle |
|        | POST      | oauth/token/refresh                     |                 | Laravel\Passport\Http\Controllers\TransientTokenController@refresh        | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | oauth/tokens                            |                 | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@forUser | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | oauth/tokens/{token_id}                 |                 | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@destroy | Barryvdh\Cors\HandleCors::class,web,auth |

oauth/ というrouteがいくつか生成されている。
laravelでは通常、apiは api/ というrouteになるため、こちらのrouteに関してもapi/ にしたい。

手順

app_name/app/Providers/AuthServiceProvider.phpboot() を変更する。

デフォルトは以下のようになっている。

<?php

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

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();
    }
}

このファイルの

    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }

    public function boot()
    {
        $this->registerPolicies();
        $callback = null;
        $options = [
            'prefix' => 'api/oauth',
            'namespace' => '\Laravel\Passport\Http\Controllers',
        ];
        Passport::routes($callback, $options);
    }

にする。

これで、routeのprefixが oauth/ から api/oauth に変更された。

php artisan route:list で確認するとこんな感じ。

# php artisan route:list

|        | POST      | api/oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\ApproveAuthorizationController@approve  | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | api/oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\AuthorizationController@authorize       | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | api/oauth/authorize                         |                 | Laravel\Passport\Http\Controllers\DenyAuthorizationController@deny        | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | api/oauth/clients                           |                 | Laravel\Passport\Http\Controllers\ClientController@forUser                | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | api/oauth/clients                           |                 | Laravel\Passport\Http\Controllers\ClientController@store                  | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | api/oauth/clients/{client_id}               |                 | Laravel\Passport\Http\Controllers\ClientController@destroy                | Barryvdh\Cors\HandleCors::class,web,auth |
|        | PUT       | api/oauth/clients/{client_id}               |                 | Laravel\Passport\Http\Controllers\ClientController@update                 | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | api/oauth/personal-access-tokens            |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@forUser   | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | api/oauth/personal-access-tokens            |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@store     | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | api/oauth/personal-access-tokens/{token_id} |                 | Laravel\Passport\Http\Controllers\PersonalAccessTokenController@destroy   | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | api/oauth/scopes                            |                 | Laravel\Passport\Http\Controllers\ScopeController@all                     | Barryvdh\Cors\HandleCors::class,web,auth |
|        | POST      | api/oauth/token                             |                 | Laravel\Passport\Http\Controllers\AccessTokenController@issueToken        | Barryvdh\Cors\HandleCors::class,throttle |
|        | POST      | api/oauth/token/refresh                     |                 | Laravel\Passport\Http\Controllers\TransientTokenController@refresh        | Barryvdh\Cors\HandleCors::class,web,auth |
|        | GET|HEAD  | api/oauth/tokens                            |                 | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@forUser | Barryvdh\Cors\HandleCors::class,web,auth |
|        | DELETE    | api/oauth/tokens/{token_id}                 |                 | Laravel\Passport\Http\Controllers\AuthorizedAccessTokenController@destroy | Barryvdh\Cors\HandleCors::class,web,auth |
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?