Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Piro's avatar
Level 1

TOKEN JWT error token_not_provided

I set up my file .htaccess how the wiki of jwt-auth explain. But i get the error token_not_provided. What happening??

0 likes
11 replies
Piro's avatar
Level 1

I'am developing an app for IOS based in Swift Language, i need to authenticate user. I need to send the headers, but i don't know how do it.

here my structures

CartasController.php

public function __construct()
    {
        $this->middleware('jwt.auth');
    }



    public function index() {

        // dd(\Request::all());

        // return view('cartas');

        echo "ÍNDICE DA CONTROLLER CARTAS";
        
    }

    public function getCarta() {

        // $carta = Cartas::where('carta_id', 1)->get();

        // echo json_encode($carta);

        //echo json_encode($dados);

        echo "FUNÇÃO GETCARTA DA CONTROLLER CARTA";


    }

routes.php

Route::get('/', 'WelcomeController@index');

Route::get('home', 'HomeController@index');

Route::get('cartas/', 'CartasController@index');

Route::get('cartas/getCarta', 'CartasController@getCarta');

Route::post('cartas/dados', 'CartasController@dados');

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

somebody can help me?

mehany's avatar

@Piro there are two options here, you can provide the token as a parameter or in the requests' headers. There is no specific implementation for swift. Something like so should work

// set up the base64-encoded credentials
let username = "user"
let password = "pass"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)
let base64LoginString = loginData.base64EncodedStringWithOptions(nil)

// create the request
let url = NSURL(string: "http://www.example.com/")
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")

// fire off the request
// make sure your class conforms to NSURLConnectionDelegate
let urlConnection = NSURLConnection(request: request, delegate: self)

Source

1 like
Piro's avatar
Level 1

@mehany Thank you!!! I think it's work fine. I will try here...

Piro's avatar
Level 1

can you help me with JWT-AUTH component? Before i try in IOS request, i want to do in web interface. I'am trying a simple POST request in route...

Route::post('cartas/getCarta', 'CartasController@getCarta');

and my controller...

public function __construct()
    {
        $this->middleware('jwt.auth');
    }

    public function getCarta() {
        // $carta = Cartas::where('carta_id', 1)->get();
        // echo json_encode($carta);
        //echo json_encode($dados);
        echo "FUNÇÃO GETCARTA DA CONTROLLER CARTA";
    }

when set post in route i get this error... >>> MethodNotAllowedHttpException in RouteCollection.php line 207:

when i set get in route i get this >>> {"error":"token_not_provided"}

what happen??

mehany's avatar

do you have CORS in place? and how are you providing the token to the controller? I am assuming you are using this package , I recommend you to refer to the docs for testing it.

1 like
Piro's avatar
Level 1

Yap. I'am using exactly this pacage. Than, i'am new in laravel and i've so much to learn about. I want to do a post request for this controller

Route::post('cartas/getCarta', 'CartasController@getCarta');

just to test and learn how it works. I need to set the CORS, but i don't know how do it without a view. I want to request directly on this controller, like a ajax request.

mehany's avatar

@piro on this page there is a working example to get you started but I would do lots of readings first or you will not get much programming done! I haven't implemented CORS in L5 yet, but I found this thread taking about it. Hope that helped!

1 like
Piro's avatar
Level 1

@mehany Thank you buddy.. i'll see. And thank you so much for the help with swift request implementation. It will really help me.

Piro's avatar
Level 1

@mehany here i'am again kkkkkkk. How can i use the println() to show what have in urlConnection??

nabaz's avatar

Just add AUTH_PROTECT_API=false to your .env file

Please or to participate in this conversation.