Laravel jwt-auth我不使用标准Laravel用户表

Laravel没有用户标准的用户模型和用户表。我之前的程序员是这样规划的。我想添加jwt来将api分发到项目中。

保留用户的表结构如下(表名client_users)

  • @property int $ id
  • @property int | null $ client_id
  • @属性字符串|空$ client_name
  • @property string |空$ client_pass
  • @property string | null $ client_phone
  • @property string |空$ client_mail
  • @property string |空$ api_token
  • @属性字符串|空$ remember_token
  • @property string |空$ visible
  • @属性字符串|空$ deleted_at

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

模型文件如下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 = [];

}

配置身份验证文件如下。 DIR config / 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,
            ],
        ],
    ];

控制器部分的不同之处在于,我们使用客户电话和密码而不是标准电子邮件密码进行查询

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

感谢您的建议和帮助。最好的祝福。

评论
  • caut
    caut 回复

    Your code can't work because when you call auth(), it uses the default authentication guard, which is web in your case but this one does not exist. You need to replace it by api. Therefore, calling auth() will be exactly the same than auth('api')