I've been facing this weird error... I set up my laravel passport auth, and login works and provides a token. I'm trying to 'guard' my routes so that only logged in users can access them. I am using 'auth:api' middleware, when a token is provided everything goes well as it should but when it's not I keep getting 'The GET method is not supported for this route. Supported methods: POST.' error and I can't seem to figure out why.
here is my Authenticate.php
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
}
here are the routes I have set up
Route::group([
'prefix' => 'auth'
], function () {
Route::post('login', 'Auth\AuthController@login')->name('login');
Route::post('register', 'Auth\AuthController@register');
Route::group([
'middleware' => 'auth:api'
], function () {
Route::get('logout', 'Auth\AuthController@logout');
Route::get('user', 'Auth\AuthController@user');
});
});
Route::get('check', function () {
return response()->json([
'message' => 'YOU'RE IN!'
], 200);
})->middleware('auth:api');
and here is my authController
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
class AuthController extends Controller
{
//
public function login(Request $request)
{
$request->validate([
'email' => 'required|string|email',
'password' => 'required|string'
]);
$credentials = request(['email', 'password']);
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(1);
$token->save();
return response()->json([
'user' => Auth::user(),
'access_token' => $tokenResult->accessToken,
'token_type' => 'Bearer',
'expires_at' => Carbon::parse(
$tokenResult->token->expires_at
)->toDateTimeString()
], 200);
}
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
'fName' => 'required|string',
'lName' => 'required|string',
'email' => 'required|string|email|unique:users',
'password' => 'required|string'
]);
if ($validator->fails()) {
$error = $validator->errors()->first();
return response()->json([
'message' => $error
], 404);
}
$user = new User;
$user->first_name = $request->fName;
$user->last_name = $request->lName;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->save();
return response()->json([
'message' => 'Successfully created user!'
], 201);
}
public function logout(Request $request)
{
$request->user()->token()->revoke();
return response()->json([
'message' => 'Successfully logged out'
], 200);
// Auth::logout();
}
public function user(Request $request)
{
return response()->json($request->user());
}
}
when I send my request with a header containing the token it works and I get the "YOU'RE IN" message, when I don't I get the error. I did try to send it as both a POST and a GET request which in fact shouldn't be needed since I have it declared as a GET route.
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST. in file D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php on line 117
#0 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php(103): Illuminate\Routing\AbstractRouteCollection->methodNotAllowed(Array, 'GET')
#1 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php(40): Illuminate\Routing\AbstractRouteCollection->getRouteForMethods(Object(Illuminate\Http\Request), Array)
#2 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php(162): Illuminate\Routing\AbstractRouteCollection->handleMatchedRoute(Object(Illuminate\Http\Request), NULL)
#3 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\Router.php(639): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request))
#4 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\Router.php(628): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#5 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\Router.php(617): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#6 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(165): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#7 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#8 D:\My Workspace\Laravel\awiz\app\Http\Middleware\Cors.php(18): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): App\Http\Middleware\Cors->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#12 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#13 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#14 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(63): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#17 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#18 D:\My Workspace\Laravel\awiz\vendor\fruitcake\laravel-cors\src\HandleCors.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#20 D:\My Workspace\Laravel\awiz\vendor\fideloper\proxy\src\TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#21 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#22 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#23 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(140): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#24 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(109): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#25 D:\My Workspace\Laravel\awiz\public\index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#26 D:\My Workspace\Laravel\awiz\server.php(21): require_once('D:\\My Workspace...')
#27 {main}
EDIT
I did, in fact, try to change the route the middleware is supposed to redirect me to which in turn caused me to face another error... It says the route isn't defined but am pretty sure it is...
Route::get('loginfailed', function () {
return response()->json(['error' => 'unauthenticated']);
});
this is my route definition and this is the new redirectTo function
protected function redirectTo($request)
{
if (!$request->expectsJson()) {
return route('loginfailed');
}
}
this is the error I get now
Symfony\Component\Routing\Exception\RouteNotFoundException: Route [loginfailed] not defined. in file D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php on line 420
#0 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php(768): Illuminate\Routing\UrlGenerator->route('loginfailed', Array, true)
#1 D:\My Workspace\Laravel\awiz\app\Http\Middleware\Authenticate.php(18): route('loginfailed')