If follow Step by Step Instrations From jwt-auth
I Same Copy Past This AuthConstroller
$token = auth()->attempt($credentials);
Error Message: Call to undefined function App\Http\Controllersauth()
| Q | A
| ----------------- | ---
| Bug? | no / yes
| New Feature? | no / yes
| Framework | Lumen
| Framework version | 5.7.7
| Package version | 1.0.0-rc.2
| PHP version | 7.2.9
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Model\User;
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
/**
* Get a JWT via given credentials.
*
* @return \Illuminate\Http\JsonResponse
*/
public function login(Request $request)
{
// $credentials = request(['email', 'password']);
$credentials = $request->only('email', 'password');
if (! $token = auth()->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
}
Add this function to your class
public function guard() {
return Auth::guard('api');
}
You can then use $this->guard instead of auth():
$this->guard()->attempt($credentials)
This is my implementation for Lumen 5.8 for UnitTest JWT.
````
namespace AppTraits;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth;
/**
/**
* This method add the header Authorization with Bearer and token.
*
* @param Authenticatable $user user to attempt authentication
* @param string $method http method Like 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'
* @param string $uri the url to consume
* @param array $data
* @param array $headers extra headers that can be added to a the request.
* @return mixed
*/
protected function apiAs(Authenticatable $user, $method, $uri, array $data = [], array $headers = [])
{
$token = Auth::guard('api')->login($user);
$headers = array_merge([
'Authorization' => 'Bearer '.$token,
], $headers);
return $this->json($method, $uri, $data, $headers);
}
}
````
Most helpful comment
Add this function to your class
You can then use
$this->guardinstead ofauth():