我已经在Laravel-5.8中编写了POST Request端点用于用户登录。 这是文件的位置:
localhost:8888 / tsl-clientportal-app / clientportal-backend
AuthCoontroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\User;
use App\Notifications\SignupActivate;
use Avatar;
use Storage;
class AuthController extends Controller
{
public function login(Request $request)
{
$request->validate([
'email' => 'required|string|email',
'password' => 'required|string',
'remember_me' => 'boolean'
]);
$credentials = request(['email', 'password']);
$credentials['active'] = 1;
$credentials['deleted_at'] = null;
if(!Auth::attempt($credentials))
return response()->json([
'message' => 'Unauthorized'
], 401);
$user = $request->user();
$tokenResult = $user->createToken('Personal Access Token');
$token = $tokenResult->token;
if ($request->remember_me)
$token->expires_at = Carbon::now()->addWeeks(13);
$token->save();
return response()->json([
'access_token' => $tokenResult->accessToken,
'token_type' => 'Bearer',
'expires_at' => Carbon::parse($tokenResult->token->expires_at)->toDateTimeString(),
'data' => $user
], 200);
}
}
api.php
Route::group([
'prefix' => 'auth'
], function () {
Route::post('login', 'AuthController@login');
Route::post('signup', 'AuthController@signup');
Route::get('signup/activate/{token}', 'AuthController@signupActivate');
Route::group([
'middleware' => 'auth:api'
], function() {
Route::get('logout', 'AuthController@logout');
Route::get('user', 'AuthController@user');
});
});
当我在POSTMAN上测试Request时,我希望看到成功的结果。但是我收到了下图所示的错误:
答案 0 :(得分:0)
Laravel Web路由在请求标头中需要“ XSRF-TOKEN”,因此,对于在POSTMAN中进行测试,您应在请求标头中的Cookie密钥中发送“ XSRF-TOKEN = value”。
答案 1 :(得分:0)
一般来说(不仅限于这个问题)它可以有很多原因,最常见的是 your connection to your db is disable
。
例如在 xampp 中,您应该启动 Mysql 或其他数据库...
是的。它太简单了,但却是最常见的错误:)