'401 Unauthorized only occurring on some pages in Laravel 8
Have created an app in Laravel 8, and have used the PHP artisan command "ui:auth" to create a login system, which works fine. However, when trying to use "auth()->check()" in other controllers, I get a 401 Unauthorised Error. This occurs both when logged in in the app, as in Postman.
My Controller that is having the issue:
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Store;
use Illuminate\Support\Facades\Auth;
class StoreController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @return \Illuminate\Http\Response
*/
public function show()
{
return auth()->user();
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Solution 1:[1]
The issue ended up being resolved by updating /app/Http/Kernel.php and adding the following code:
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
to protected $middlewareGroups
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
Solution 2:[2]
It should work for you. Try this: Carefully enter the right credentials.
return Auth::user();
Solution 3:[3]
Laravel's default authentication system is on session and you can not log in with postman. To do this, you must use a passport or sanctum reader. Then you have to go to "config/auth.php" and set the following values
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
You must use "api guards"
Read the link below
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | James M |
Solution 2 | |
Solution 3 | sina khaghani |