douqian2957 2016-12-10 08:12
浏览 203

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

I have a mobile application(mobile_users) and a backend to manage the system(system_users) i want to manage them in separate tables so basically to communicate with the mobile application i'm trying to use an api with JWT enabled and for the backend i'm using default auth method in laravel.

My problem is when i try to change the auth table for JWT it keeps on accessing the system_users table, even though the documentation says you can change the default User model path i tried changing but no luck,

Please help me with this,

I'm using https://github.com/tymondesigns/jwt-auth this library for JWT,

My Database Structure will be,

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条回答 默认 最新

  • drdr123456 2016-12-10 08:37
    关注

    For different authentication, there are library already exist. check this, it will solve your problem https://github.com/Hesto/multi-auth

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧