Jwt-auth: Call to undefined function auth()

Created on 30 Dec 2018  路  2Comments  路  Source: tymondesigns/jwt-auth

Call to undefined function App\Http\Controllersauth()

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()

Your environment

| 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);
    }
}

Most helpful comment

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)

All 2 comments

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;

/**

  • Trait AuthenticationForUnitTest
  • This is used to achieve authentication in unit test.
  • @package AppTraits
    */
    trait AuthenticationForUnitTest
    {
/**
 * 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);
}

}

````

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lbottoni picture lbottoni  路  3Comments

gamelife1314 picture gamelife1314  路  3Comments

agneshoving picture agneshoving  路  3Comments

Rasoul-Karimi picture Rasoul-Karimi  路  3Comments

therealmjk picture therealmjk  路  3Comments