簡體   English   中英

Laravel jwt-auth 我不使用標准 Laravel 用戶表

[英]Laravel jwt-auth I Dont Use a Standard Laravel User Table

Laravel 沒有用戶標准用戶 model 和用戶表。 我之前的程序員是這樣計划的。我想添加 jwt 以將 api 分發到項目中。

保存用戶的表結構如下(表名client_users

  • @property int $id
  • @property int|null $client_id
  • @property 字符串|null $client_name
  • @property 字符串|null $client_pass
  • @property 字符串|null $client_phone
  • @property 字符串|null $client_mail
  • @property 字符串|null $api_token
  • @property 字符串|null $remember_token
  • @property 字符串|null $可見
  • @property 字符串|null $deleted_at

首先,代替User model使用的文件如下。

model文件如下ClientUser.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
// use Illuminate\Foundation\Auth\User as Authenticatable;
// class User extends Authenticatable implements JWTSubject

class ClientUser extends Model implements JWTSubject
{

    protected $connection = 'remoteMysql';

    protected $table = "client_users";

    protected $guarded = [];

}

配置身份驗證文件如下。 目錄配置/auth.php

<?php    
    return [
        'defaults' => [
            'guard' => 'web',
            'passwords' => 'users',
        ],
        'guards' => [
            'api' => [
                'driver' => 'jwt',
                'provider' => 'users',
            ],
        ],

        'providers' => [
            'users' => [
                'driver' => 'eloquent',
                'model' => \App\ClientUser::class,
            ],
        ],

        'passwords' => [
            'users' => [
                'provider' => 'users',
                'table' => 'password_resets',
                'expire' => 60,
            ],
        ],
    ];

controller部分的不同之處在於我們使用客戶電話和密碼而不是標准的email密碼來查詢

Api/AuthController.php文件如下

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login']]);
    }

    /**
     * Get a JWT via given credentials.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function login()
    {
        $credentials = request(['client_phone', 'client_pass']);

        if (! $token = auth()->attempt($credentials)) {
            return response()->json(['error' => 'Unauthorized'], 401);
        }

        return $this->respondWithToken($token);
    }

    /**
     * Get the authenticated User.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function me()
    {
        return response()->json(auth()->user());
    }

    /**
     * Log the user out (Invalidate the token).
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        auth()->logout();

        return response()->json(['message' => 'Successfully logged out']);
    }

    /**
     * Refresh a token.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function refresh()
    {
        return $this->respondWithToken(auth()->refresh());
    }

    /**
     * Get the token array structure.
     *
     * @param  string $token
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function respondWithToken($token)
    {
        return response()->json([
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth()->factory()->getTTL() * 60
        ]);
    }
}

感謝您的建議和幫助。 此致。

您的代碼無法工作,因為當您調用auth()時,它使用默認的身份驗證保護,在您的情況下為web但這個不存在。 您需要將其替換為api 因此,調用auth()將與auth('api')完全相同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM