DEV Community

Ahmad9250
Ahmad9250

Posted on

Login form issue in Laravel blade using API Sanctum

I got an issue with the Laravel blade login form when I try to login through in postman using login API it works fine and my data displays through the API token I'm using Laravel Sanctum in this project when I try to use the same method in Laravel blade the server starts to get down and my API in postman also stops working. I send my user data through seeder and I'm trying to log in through the user table email and password that are present in MYSQL.

My API routes:

`
Route::post("login",[UserController::class,'index']);

Route::group(['middleware' => 'auth:sanctum'], function(){

//Employee routes
Route::get('employees',[Employeecontroller::class,'index']);
Route::get('employees/{id}',[Employeecontroller::class,'show']);
Route::post('employees',[Employeecontroller::class,'store']);
Route::put('employees/{id}',[Employeecontroller::class,'update']);
Route::delete('employees/{id}',[Employeecontroller::class,'destroy']);

////designation controller route
Route::get('designations',[DesignationController::class,'index']);
Route::get('designations/{id}',[DesignationController::class,'show']);
Route::post('designations',[DesignationController::class,'store']);
Route::put('designations/{id}',[DesignationController::class,'update']);
Route::delete('designations/{id}',[DesignationController::class,'destroy']);

//employee metadata routes
Route::get('metadata',[EmployeeMetaDataController::class,'index']);
Route::get('metadata/{id}',[EmployeeMetaDataController::class,'show']);
Route::post('metadata',[EmployeeMetaDataController::class,'store']);
Route::put('metadata/{id}',[EmployeeMetaDataController::class,'update']);
Route::delete('metadata/{id}',[EmployeeMetaDataController::class,'destroy']);

//department routes
Route::get('department',[DepartmentController::class,'index']);
Route::get('department/{id}',[DepartmentController::class,'show']);
Route::post('department',[DepartmentController::class,'store']);
Route::put('department/{id}',[DepartmentController::class,'update']);
Route::delete('department/{id}',[DepartmentController::class,'destroy']);

});
Enter fullscreen mode Exit fullscreen mode

`
My Web Routes:

Route::get('login',[logincontroller::class,'login'])->name('login');
Route::post('Api',[logincontroller::class,'Api'])->name('Api');

My login blade:

<form method="post" action="{{route('Api')}}" >
@csrf
<!-- Email input -->
<div class="form-outline mb-4 mt-5">
<input placeholder="Enter email" type="text" name="email" id="loginName" class="form-control" />
</div>
<!-- Password input -->
<div class="form-outline mb-4">
<input placeholder="Enter password" type="text" name="password" id="loginPassword" class="form-control" />
</div>
<!-- Submit button -->
<button type="submit" class="btn btn-primary btn-block mb-4">Sign in</button>
</form>

My Login Controller:

`public function Api(LoginRequest $request){
$http = new Client;

    $email=$request->email;
    $password=$request->password;
    $response = $http->request('POST', 'http://127.0.0.1:8000/api/login', [
        'headers' => [
            'Authorization' => 'Bearer'.session()->get('token.access_token'),
            'Accept' => 'application/json',
        ],
        'query'=>[
            'email'=>$email,
            'password'=>$password
        ]
    ]);

    $result=json_decode((string)$response->getBody(),true);
    return dd($result);

return view('login');
Enter fullscreen mode Exit fullscreen mode

}`

My User Controller:

`function index(Request $request)
{
$user= User::where('email', $request->email)->first();
// print_r($data);
if (!$user || !Hash::check($request->password, $user->password)) {
return response([
'message' => ['These credentials do not match our records.']
], 404);
}

         $token = $user->createToken('my-app-token')->plainTextToken;

        $response = [
            'user' => $user,
            'token' => $token
        ];

         return response($response, 201);
}`
Enter fullscreen mode Exit fullscreen mode

Top comments (0)