mannysoft/api-auth

Laravel 扩展 Laravel Passport 的包

0.3 2019-10-16 02:16 UTC

This package is auto-updated.

Last update: 2024-09-16 13:17:44 UTC


README

用于 API 认证的 Laravel 包。

安装

使用 composer 安装此包。

composer require mannysoft/api-auth
php artisan migrate
php artisan passport:install
php artisan passport:keys
php artisan vendor:publish --provider="Mannysoft\ApiAuth\ServiceProvider" --tag="migrations"

打开你的 oauth_clients 表,查找 password_client

修改你的 .env 文件

APP_OAUTH_CLIENT_ID=

APP_OAUTH_CLIENT_SECRET=

Laravel 5.5 使用包自动发现,因此不需要你手动添加 ServiceProvider

Laravel\Passport\HasApiTokens 特性添加到你的 App\User 模型中。这个特性将为你的模型提供一些辅助方法,允许你检查认证用户的令牌和作用域

<?php

namespace App;

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

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;
}

如果你使用 Facebook 登录,请更新你的 config/services.php

'facebook' => [
    'client_id' => env('FACEBOOK_CLIENT_ID'),
    'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
    'redirect' => env('FACEBOOK_REDIRECT'),
],

根据你的需求更新 config/api-auth.php

最后,在你的 config/auth.php 配置文件中,你应该将 API 认证守卫的驱动选项设置为 passport。这将指示你的应用程序在认证传入的 API 请求时使用 Passport 的 TokenGuard

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

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

重置密码邮件

要开始,覆盖你的 User 模型中的 sendPasswordResetNotification 方法。在这个方法中,你可以使用你选择的任何通知类来发送通知。密码重置的 $token 是该方法收到的第一个参数

/**
 * Send the password reset notification.
 *
 * @param  string  $token
 * @return void
 */
public function sendPasswordResetNotification($token)
{
    $this->notify(new \Mannysoft\ApiAuth\Notifications\ResetPasswordNotification($token, $this->email));
}

可用的路由

配置