需要帮助,如何在登录后不使用数据库的情况下获取JWT令牌我尝试使用tymondesigns / jwt-auth,但是它给我“在null上调用成员函数send()”错误。
这是我在AuthController上的代码(构造和登录功能)
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = auth()->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
我正在使用自定义中间件:
namespace App\Http\Middleware;
use Closure;
use Session;
use Illuminate\Http\Request;
class CustomMiddleware
{
protected $loginPath = 'login';
public function handle($request, Closure $next)
{
if (!empty(session('login')))
{
$logged_in = $request->session()->put('logged_in');
if (!$logged_in)
{
return redirect()->guest('login')->with('flag','1');
}
return $next($request);
}
}
这是我在api.php上的路线
Route::group([
'middleware' => 'custom',
'prefix' => 'auth'
], function ($router) {
Route::post('login', 'AuthController@login');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
});
在kernel.php中,我放了:
'custom' => \App\Http\Middleware\CustomMiddleware::class,
答案 0 :(得分:1)
根据文档安装JWT后,您应该使用
$token = JWTAuth::attempt($credentials)
代替$token = auth()->attempt($credentials)