如何在Laravel中有两个不同的表进行身份验证? (JWT)

时间:2016-12-10 08:12:21

标签: php laravel jwt laravel-5.3

我有一个移动应用程序(mobile_users)和一个管理系统的后端(system_users)我想在单独的表中管理它们,所以基本上要与移动应用程序通信我尝试使用启用了JWT的api和后端我在laravel中使用默认的auth方法。

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

请帮助我,

我将https://github.com/tymondesigns/jwt-auth此库用于JWT,

我的数据库结构将是,

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',

1 个答案:

答案 0 :(得分:0)

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