【问题标题】:How to have two different tables for authentication in Laravel? (JWT)如何在 Laravel 中有两个不同的表进行身份验证? (智威汤逊)
【发布时间】:2017-04-25 16:55:24
【问题描述】:

我有一个移动应用程序(mobile_users)和一个后端来管理系统(system_users)我想在单独的表中管理它们,所以基本上是为了与我正在尝试使用的移动应用程序进行通信一个启用了JWT 的 api,对于后端我使用 laravel 中的默认身份验证方法。

我的问题是当我尝试更改 JWT 的 auth 表时,它继续访问 system_users 表,即使文档说您可以更改默认 用户模型路径 我尝试更改但没有运气,

请帮帮我,

我正在为 JWT 使用 https://github.com/tymondesigns/jwt-auth 这个库,

我的数据库结构将是,

mobile_users_table = authentication using JWT

system_users_table = default laravel auth

api.php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/


Route::post('auth', 'Api\v1\AuthController@authenticate');
Route::get('auth/me', 'Api\v1\AuthController@getAuthenticatedUser');

Route::get('validate', function () {
    return "cool";
})->middleware('jwt.auth');

AppUser.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AppUser extends Model
{
    //
}

AuthController.php

namespace App\Http\Controllers\Api\v1;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class AuthController extends Controller
{
    public function authenticate(Request $request)
    {
        // grab credentials from the request
        $credentials = $request->only('email', 'password');

        try {
            // attempt to verify the credentials and create a token for the user
            if (! $token = JWTAuth::attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        } catch (JWTException $e) {
            // something went wrong whilst attempting to encode the token
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        // all good so return the token
        return response()->json(compact('token'));
    }


        public function getAuthenticatedUser()
        {
                try {

                        if (! $user = JWTAuth::parseToken()->authenticate()) {
                                return response()->json(['user_not_found'], 404);
                        }

                } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {

                        return response()->json(['token_expired'], $e->getStatusCode());

                } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {

                        return response()->json(['token_invalid'], $e->getStatusCode());

                } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {

                        return response()->json(['token_absent'], $e->getStatusCode());

                }

                // the token is valid and we have found the user via the sub claim
                return response()->json(compact('user'));
        }
}

jwt.php

/*
    |--------------------------------------------------------------------------
    | User Model namespace
    |--------------------------------------------------------------------------
    |
    | Specify the full namespace to your User model.
    | e.g. 'Acme\Entities\User'
    |
    */

    'user' => 'App\AppUser',

【问题讨论】:

  • @goto 请看一下

标签: php laravel jwt laravel-5.3


【解决方案1】:

对于不同的身份验证,已经存在库。检查一下,它会解决你的问题https://github.com/Hesto/multi-auth

【讨论】:

  • 你的意思是没有图书馆是不可能的?..或者有任何其他方法?
  • 没有库,你可以做到。 Laravel 有一个概念Guard,它会根据角色来处理用户。
猜你喜欢
  • 1970-01-01
  • 2018-07-23
  • 1970-01-01
  • 2016-09-21
  • 2018-07-14
  • 2015-10-16
  • 2021-07-15
  • 2017-01-30
  • 2020-12-27
相关资源
最近更新 更多