我是Lumen的新手。我正在创建需要对每个用户进行身份验证的API。我遵循了Lumen文档中显示的所有步骤。但是,尽管我提供了有效的参数,但所有API都作为响应未经授权返回。
在 routes.php 中,我添加了以下代码,
<?php
$api = app('Dingo\Api\Routing\Router');
$api->post('user/getprofiledetails', ['middleware' => 'auth:api', 'uses' => 'UserController@getProfileDetails']);
?>
在 authenticate.php ,
中<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;
class Authenticate
{
/**
* The authentication guard factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($this->auth->guard($guard)->guest()) {
return response('Unauthorized.', 401);
}
return $next($request);
}
}
在 UserController.php
中<?php
namespace App\Http\Controllers;
use Auth;
use Validator;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use GuzzleHttp\Exception\ClientException;
use Dingo\Api\Exception\ValidationHttpException;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
public function getProfileDetails($user_type, $username)
{
if(Auth::check()) {
echo "You are a authenticated user";
}
else
{
echo "You are not authorized to login";
}
}
任何帮助都将非常感激。谢谢。