Setup Laravel Sactum api Login
I'm trying to use Sanctum for a mobile app login and functionality, however, I can't seem to be able to use a controller along with the api routes successfully, this is the original route from the docs
Route::post('/sanctum/token', function (Request $request) {
$request->validate([
'scard' => 'required',
'password' => 'required',
'device_name' => 'required'
]);
$user = User::where('scard', $request->scard)->first();
if (! $user || ! Hash::check($request->password, $user->password)) {
throw ValidationException::withMessages([
'email' => ['The provided credentials are incorrect.'],
]);
}
$token = $user->createToken($request->device_name)->plainTextToken;
$response = [
'user' => $user,
'token' => $token
];
return response($response, 201);
});
I wanted to move this into a LoginController for api and turn it into this route
Route::post('/login','API\LoginController@login');
and this method in the API\LoginController.php
public function login(Request $request) {
$request->validate([
'scard' => 'required',
'password' => 'required',
'device_name' => 'required'
]);
$user = User::where('scard', $request->scard)->first();
if (! $user || ! Hash::check($request->password, $user->password)) {
throw ValidationException::withMessages([
'email' => ['The provided credentials are incorrect.'],
]);
}
$token = $user->createToken($request->device_name)->plainTextToken;
$response = [
'user' => $user,
'token' => $token
];
return response($response, 201);
}
The problem is when I try and check this route using Insomnia or Postman, I get these errors:
"message": "",
"exception": "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php",
"line": 179,
"trace": [
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"line": 635,
"function": "match",
"class": "Illuminate\Routing\RouteCollection",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"line": 624,
"function": "findRoute",
"class": "Illuminate\Routing\Router",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
"line": 613,
"function": "dispatchToRoute",
"class": "Illuminate\Routing\Router",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
"line": 170,
"function": "dispatch",
"class": "Illuminate\Routing\Router",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 130,
"function": "Illuminate\Foundation\Http\{closure}",
"class": "Illuminate\Foundation\Http\Kernel",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\inertiajs\inertia-laravel\src\Middleware.php",
"line": 13,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 171,
"function": "handle",
"class": "Inertia\Middleware",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php",
"line": 21,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 171,
"function": "handle",
"class": "Illuminate\Foundation\Http\Middleware\TransformsRequest",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php",
"line": 21,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 171,
"function": "handle",
"class": "Illuminate\Foundation\Http\Middleware\TransformsRequest",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php",
"line": 27,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 171,
"function": "handle",
"class": "Illuminate\Foundation\Http\Middleware\ValidatePostSize",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php",
"line": 63,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 171,
"function": "handle",
"class": "Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\fideloper\proxy\src\TrustProxies.php",
"line": 57,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 171,
"function": "handle",
"class": "Fideloper\Proxy\TrustProxies",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
"line": 105,
"function": "Illuminate\Pipeline\{closure}",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
"line": 145,
"function": "then",
"class": "Illuminate\Pipeline\Pipeline",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
"line": 110,
"function": "sendRequestThroughRouter",
"class": "Illuminate\Foundation\Http\Kernel",
"type": "->"
},
{
"file": "C:\xampp\htdocs\sanctum\public\index.php",
"line": 55,
"function": "handle",
"class": "Illuminate\Foundation\Http\Kernel",
"type": "->"
}
]
I'm not sure why this error is happening or how to solve it, it used to say that this LoginController didn't exist but I than deleted it and created it with this command php artisan make:controller API/LoginController --api so now I don't get that missing controller but now I get no message at all
Please or to participate in this conversation.