tuupola / slim-jwt-auth

PSR-7 and PSR-15 JWT Authentication Middleware
https://appelsiini.net/projects/slim-jwt-auth
MIT License
827 stars 141 forks source link

Wrong number of segments - Help #205

Closed sergioas closed 3 years ago

sergioas commented 3 years ago

I'm using jwt tuupola with Slim, like this:

$app->add(new Tuupola\Middleware\JwtAuthentication([ "path" => [ "xxxxxxxxxxx", "xxxxxxxxxxx", ], "secret" => getenv('XXXXXXXXXXXX'), "error" => function ($response, $arguments) { $data["status"] = "error"; $data["message"] = $arguments["message"]; return $response ->withHeader("Content-Type", "application/json") ->getBody()->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); } ]));

ok, it's working

but, when the user logs on to the page of the site, and goes to another page that needs to post posts validated by jwt, the error "Wrong number of segments" occurs, what do I need to do?

tuupola commented 3 years ago

Show the curl request you are doing. You might have a broken token.

sergioas commented 3 years ago

ok, follow the ajax code: at login I add the token on localstorage: $.ajax({ url: "../../slim/loginUser", method :"post", dataType: "json", data:dados , context: document.body }).done(function(data ) {

if(data.erro != 'false' && data.erro != false){ alert(data.erro); } else{

localStorage.setItem('token', data.token);

} }) .fail(function(xhr, textStatus, errorThrown) { alert( 'STATUS: '+textStatus+'\nERROR THROWN: '+errorThrown );

}) .always(function() { alert('always'); });

then, the user is logged in, and can browse the internal pages of the control panel, but when he is there inside the control panel, when making this request:

$.ajax({ url: urlService, method :"post", dataType: "json", data:xxx , beforeSend: function (xhr) { //Include the bearer token in header xhr.setRequestHeader("Authorization", 'Bearer '+ localStorage.getItem('token')); }, context: document.body }).done(function(data ) {

}) .fail(function() { alert( "FAIL" );
}) .always(function() {

});

in short, he logged in: "www.canaldajoia.com.br/vender" and, after logging in, it stays on this page ... but, being logged in and go to: "www.canaldajoia.com.br/admin/user", the problem occurs for any request

tuupola commented 3 years ago

I need to see the actual request JavaScript is doing. If not the whole request at least the the Authorization header.

You can use ``` to format code blocks.

sergioas commented 3 years ago

tuupola, I found the error, after you asked me about the token ... my site has two places to log in, in the second place of login, I was not passing the token to the client ... error of logic here ... thank you for your attention, thank you very much

tuupola commented 3 years ago

Great :)