简体   繁体   English

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

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

Laravel has no user standard User model and users table. Laravel 没有用户标准用户 model 和用户表。 The programmer before me planned this way.I want to add jwt to distribute api to the project.我之前的程序员是这样计划的。我想添加 jwt 以将 api 分发到项目中。

Table structure in which users are kept is as follows (table name client_users )保存用户的表结构如下(表名client_users

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

First of all, the file used instead of the User model is as follows.首先,代替User model使用的文件如下。

The model file is as follows ClientUser.php 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 = [];

}

The config auth file is as follows.配置身份验证文件如下。 DIR config/auth.php目录配置/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,
            ],
        ],
    ];

What is different in the controller part is that we query with the customer phone and password instead of the standard email password controller部分的不同之处在于我们使用客户电话和密码而不是标准的email密码来查询

The Api/AuthController.php file is as follows 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
        ]);
    }
}

Thank you for your advice and help.感谢您的建议和帮助。 Best regards.此致。

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.您的代码无法工作,因为当您调用auth()时,它使用默认的身份验证保护,在您的情况下为web但这个不存在。 You need to replace it by api .您需要将其替换为api Therefore, calling auth() will be exactly the same than auth('api')因此,调用auth()将与auth('api')完全相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM