lulebe / php-simple-jwt-auth

6 stars 14 forks source link

Getting stuck in this step: make request with the token #1

Open DitaAjiPratama opened 3 years ago

DitaAjiPratama commented 3 years ago

I was getting stuck in here

client makes requests to protected endpoints with the header "Authorization: THETOKEN"

Did you have a sample code for this?

DitaAjiPratama commented 3 years ago

This is my client-side code:

$url    = 'http://localhost/php-simple-jwt-auth/';
$param  = 'usr=' . "testuser" . '&pw=' . hash('sha256', "mypw");

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $param);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );
$my_auth = json_decode($response)->token;

echo $my_auth;
lulebe commented 3 years ago

Hi, I don't really have a sample code because basically the whole repo is the sample code. The jwt_helper is just a class for encoding/decoding that I imported, but the other two files are really just a bare minimum implementation. So this repo was just made to show a friend how I would go about using JWTs in php.

Without knowing your exact use case, I assume you used your code to retrieve a jwt. Next thing to do would to send a request that needs authentication, this example repo provides GET /index.php for that, and add a header named "Authorization" to the request and give that header the jwt as its value.

I strongly recommend checking out other resources on this topic because I'm mainly a nodejs dev and just did this example for a friend so they could host some API for an app on a php hosting service.

I'm still very happy to know someone else might find this helpful though!

Leander