下面的教程:https://www.twilio.com/blog/build-secure-api-php-laravel-passport我设法得到Laravel/Passport安装在我的Laravel Api和Vue应用程序。
我成功创建了一个attoken:
localhost:8000/oauth/token
使登录在Postman中工作:
localhost:8000/api/login?email=jennie05@example.com&password=password
现在当我尝试注册一个用户时,我返回到主页。
我确实得到了一些未定义的方法错误从VS Code,但他们显示在登录方法,而不是在failinf register方法中:
这是控制器:
<?php
namespace AppHttpControllersAPI;
use AppHttpControllersController;
use IlluminateHttpRequest;
use AppModelsUser;
class AuthController extends Controller
{
public function register(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:55',
'email' => 'email|required|unique:users',
'password' => 'required|confirmed'
]);
$validatedData['password'] = bcrypt($request->password);
$user = User::create($validatedData);
$accessToken = $user->createToken('authToken')->accessToken;
return response([ 'user' => $user, 'access_token' => $accessToken]);
}
public function login(Request $request)
{
$loginData = $request->validate([
'email' => 'email|required',
'password' => 'required'
]);
if (!auth()->attempt($loginData)) {
return response(['message' => 'Invalid Credentials']);
}
$accessToken = auth()->user()->createToken('authToken')->accessToken;
return response(['user' => auth()->user(), 'access_token' => $accessToken]);
}
}
以下是api.php中的路由:
Route::post( 'register', 'AppHttpControllersAPIAuthController@register');
Route::post( 'login', 'AppHttpControllersAPIAuthController@login');
// Route::prefix('v2')->group(function(){ // prefix voor versie 2
Route::apiResource('/cards', 'AppHttpControllersCardController');
Route::apiResource('/games', 'AppHttpControllersGameController');
//Route::get('games', 'AppHttpControllersGameController@index')->middleware('auth:api');
//Route::post('games', 'AppHttpControllersGameController@store')->middleware('auth:api');
//Route::get('games/{id}', 'AppHttpControllersGameController@show')->middleware('auth:api');
//Route::put('games/{id}', 'AppHttpControllersGameController@update')->middleware('auth:api');
//Route::delete('games/{id}', 'AppHttpControllersGameController@destroy')->middleware('auth:api');
Route::get('/gameByPin/{pin}', 'AppHttpControllersGameController@searchPin');
Route::apiResource('/speler', 'AppHttpControllersSpelerController');
//});
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
有谁可以帮助排除这个问题吗?
邮差没有给我错误,只是重定向到主页..
哇,谢谢Patrick,你是最棒的。
刚刚检查了文档中的所有步骤。
重新运行我在设置所有内容时运行的安装命令。(显然需要这样做两次?!):
php artisan passport:install
返回:
Encryption keys already exist. Use the --force option to overwrite them.
Personal access client created successfully.
Client ID: 5
Client secret: ikJEfthxxxxxxxxxxxxxxxxxxxxxxxxxxxBjJ36
Password grant client created successfully.
Client ID: 6
Client secret: H42lpMxxxxxxxxxxxxxxxxxxxxxxxxxxxQGZgH
现在post请求的结果是一个令牌!
错误VS代码显示d使用HasApiToken"是:
Undefined type 'LaravelPassportHasApiTokens'.intelephense(1009)