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)
{
//
}
}
CodePudding user response:
It should work for you. Try this: Carefully enter the right credentials.
return Auth::user();
CodePudding user response:
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