So I am trying to create a basic JWT token authentication.
I am using Laravel as my backend API with tymon/jwt-auth for creating tokens / refreshing tokens and providing middleware to check for tokens.
I am using AngularJS with Satellizer to store the returned token in local storage and send said token for all future requests.
I am using Laravel 5.1 / Jwt-auth 0.5.* / AngularJs 1.4.7 / Satellizer (latest).
I log in and the token is stored in my local storage. I then try to make a call to authentcate's index to "get all users" and the authorization header is there with Bearer <token stored in local storage>
but I get the response of "token_not_provided" almost as if the middleware is not looking in the authorization header for the token. Here is some of my code. I am following http://ift.tt/1FNj4m6.
AuthenticateController.php:
<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\User;
class AuthenticateController extends Controller
{
public function __construct()
{
// Apply the jwt.auth middleware to all methods in this controller
// except for the authenticate method. We don't want to prevent
// the user from retrieving their token if they don't already have it
$this->middleware('jwt.auth', ['except' => ['authenticate']]);
}
public function index()
{
// Retrieve all the users in the database and return them
$users = User::all();
return $users;
}
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
try {
// verify the credentials and create a token for the user
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong
return response()->json(['error' => 'could_not_create_token'], 500);
}
// if no errors are encountered we can return a JWT
return response()->json(compact('token'));
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire