简体   繁体   English

LoginUsingId()在中间件jwt.auth,Laravel 5中不起作用

[英]LoginUsingId() not working within midleware jwt.auth, Laravel 5

This has been driving me crazy for 2 hours can't seem to fix it if I put my routes inside the jwt.auth midleware. 如果我将路线放在jwt.auth中件中,这使我疯狂了2个小时,似乎无法解决。 I want to login as another user while I am already logged in as admin from my admin dashboard using the LoginUsingId() function in Laravel, and it works all good if I dont pass the token as parameter, but I have to protect my api using the jwt token so I cant remove the midleware jwt.auth.. Is there anyway that I can make it work even the routes are inside the midleware jwt.auth? 我想以其他用户身份登录,而我已经使用Laravel中的LoginUsingId()函数从管理仪表板以admin身份登录,并且如果我不将令牌作为参数传递,那么一切正常,但是我必须使用保护我的api jwt令牌,因此我无法删除中间件jwt.auth。。即使路由位于中间件jwt.auth内,我是否还能使它工作?

Working example: 工作示例:

//Login as other User
Route::post('users/loginas/{userId}', ['as' => 'login_as', 'uses' => 'UserController@loginAs']);
Route::get('classes/{class_id}/market_feeds', ['as' => 'show_market_feeds', 'uses' => 'MarketFeedController@index']);
Route::group(['middleware' => 'jwt.auth'], function () {

})

Not working: 无法运作:

Route::group(['middleware' => 'jwt.auth'], function () {
//Login as other User
Route::post('users/loginas/{userId}', ['as' => 'login_as', 'uses' => 'UserController@loginAs']);
Route::get('classes/{class_id}/market_feeds', ['as' => 'show_market_feeds', 'uses' => 'MarketFeedController@index']);
})

My Controller for switching user looks like: 我的用于切换用户的控制器如下所示:

public function loginAs($userId)
{
    Auth::logout();
    Auth::loginUsingId($userId, true);
    return response()->json(['logged' => Auth::check(), 'user' => Auth::user()->username, 'id' => Auth::user()->id]);
}

Works good and I can see the user details... but when I try to make another call like calling this function: 效果很好,我可以看到用户详细信息...但是当我尝试进行另一个调用(如调用此函数)时:

public function displaySomethingElse($classId)
{
    return response(array('username'=>Auth::user()->username,'id' => Auth::user()->id));
}

It returns the admin user, not the specific user I want... 它返回管理员用户,而不是我想要的特定用户...

I hope I was enough clear. 我希望我足够清楚。

One approach that you can follow is to generate the token based on user object and return that. 您可以遵循的一种方法是根据用户对象生成令牌并将其返回。 And, use the returned token for the next calls. 并且,将返回的令牌用于下一次调用。

The library which you are using has an option of generating tokens based on user object 您正在使用的库具有基于用户对象生成令牌的选项

You can pass the id or any other identifier of the user that you want to login as. 您可以传递您要登录的用户的id或任何其他标识符。 Remember, that this call is as admin . 请记住,该调用是admin Receive the response token genrated from user object and use this token for your next calls. 接收从用户对象生成的响应令牌,并将此令牌用于您的下一个调用。 That makes you logged in as the user you want to login as. 这使您以要登录的用户身份登录。

Please feel free to comment, if you did not understand what I am trying to say. 如果您不明白我要说的话,请随时发表评论。

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

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