Welcome to the Kinde community.

K
A
A
A
M

Validating user token in php backend

I am using PHP as my backend, and react as frontend. How do I authenticate the user in my PHP backend, by the token I receive from the frontend (by getToken())?

K
J
5 comments

To authenticate the user in your PHP backend using the token received from the frontend, you need to follow these steps: 1. *Frontend (React) Setup:* • Use the `getToken` function from the Kinde React SDK to get the access token. • Send this token to your PHP backend in the Authorization header. Example code in React: ``` const { getToken } = useKindeAuth(); const fetchData = async () => { try { const accessToken = await getToken(); const res = await fetch('', { headers: { Authorization: `Bearer ${accessToken}` } }); const data = await res.json(); console.log(data); } catch (err) { console.error(err); } }; ``` 2. *Backend (PHP) Setup:* • In your PHP backend, you need to verify the JWT token received in the Authorization header. • Use a JWT verification library for PHP to validate the token. One popular library is `firebase/php-jwt`. Example code in PHP: ``` <?php require 'vendor/autoload.php'; use \Firebase\JWT\JWT; $jwt = $_SERVER['HTTP_AUTHORIZATION']; $jwt = str_replace('Bearer ', '', $jwt); $publicKey = file_get_contents('https://<your_subdomain>.); try { $decoded = JWT::decode($jwt, $publicKey, array('RS256')); // Token is valid, proceed with your logic echo json_encode(['status' => 'success', 'data' => $decoded]); } catch (Exception $e) { // Token is invalid http_response_code(401); echo json_encode(['status' => 'error', 'message' => 'Unauthorized']); } ?> ``` 3. *Register Your API in Kinde:* • For additional security, register your API in Kinde and ensure the audience is included in the access token. • Follow the instructions to . By following these steps, you can securely authenticate users in your PHP backend using the token received from the frontend.

but how can I know which user sent the token, get the roles, permissions etc?

To identify the user who sent the token and retrieve their roles, permissions, and other details, you can follow these steps: 1. *Extract and Verify the Token:* • Extract the token from the Authorization header in your PHP backend. • Verify the token using a JWT library like `firebase/php-jwt`. 2. *Decode the Token:* • Once verified, decode the token to access the user's information, including roles and permissions. 3. *Retrieve User Information:* • Use the decoded token to get user details such as user ID, roles, and permissions. Here is an example of how you can achieve this in PHP: *PHP Backend Code:* ``` <?php require 'vendor/autoload.php'; use \Firebase\JWT\JWT; $jwt = $_SERVER['HTTP_AUTHORIZATION']; $jwt = str_replace('Bearer ', '', $jwt); $publicKey = file_get_contents('https://<your_subdomain>.); try { $decoded = JWT::decode($jwt, $publicKey, array('RS256')); // Access user information $userId = $decoded->sub; $roles = $decoded->roles; $permissions = $decoded->permissions; // Example response echo json_encode([ 'status' => 'success', 'userId' => $userId, 'roles' => $roles, 'permissions' => $permissions ]); } catch (Exception $e) { // Token is invalid http_response_code(401); echo json_encode(['status' => 'error', 'message' => 'Unauthorized']); } ?> ``` *Frontend (React) Code:* ``` const { getToken } = useKindeAuth(); const fetchData = async () => { try { const accessToken = await getToken(); const res = await fetch('', { headers: { Authorization: `Bearer ${accessToken}` } }); const data = await res.json(); console.log(data); } catch (err) { console.error(err); } }; ``` *Documentation Links:* • • By following these steps, you can authenticate the user, retrieve their roles and permissions, and access other user details in your PHP backend.

But the public information contains n and e, how do I get a public key from that?

To authenticate the user in your PHP backend using the token received from the frontend, you can follow these steps: 1. *Frontend (React) Setup:* • Use the `getToken` function from the Kinde React SDK to get the access token. • Send this token to your PHP backend in the Authorization header. Example code in React: ``` const { getToken } = useKindeAuth(); const fetchData = async () => { try { const accessToken = await getToken(); const res = await fetch('', { headers: { Authorization: `Bearer ${accessToken}` } }); const data = await res.json(); console.log(data); } catch (err) { console.error(err); } }; ``` 2. *Backend (PHP) Setup:* • In your PHP backend, you need to verify the JWT token received in the Authorization header. • Use a JWT verification library for PHP to validate the token. One popular library is `firebase/php-jwt`. Example code in PHP: ``` <?php require 'vendor/autoload.php'; use \Firebase\JWT\JWT; $jwt = $_SERVER['HTTP_AUTHORIZATION']; $jwt = str_replace('Bearer ', '', $jwt); $publicKey = file_get_contents('https://<your_subdomain>.); try { $decoded = JWT::decode($jwt, $publicKey, array('RS256')); // Access user information $userId = $decoded->sub; $roles = $decoded->roles; $permissions = $decoded->permissions; // Example response echo json_encode([ 'status' => 'success', 'userId' => $userId, 'roles' => $roles, 'permissions' => $permissions ]); } catch (Exception $e) { // Token is invalid http_response_code(401); echo json_encode(['status' => 'error', 'message' => 'Unauthorized']); } ?> ``` 3. *Register Your API in Kinde:* • For additional security, register your API in Kinde and ensure the audience is included in the access token. • Follow the instructions to . By following these steps, you can securely authenticate users in your PHP backend using the token received from the frontend.

Add a reply
Sign up and join the conversation on Slack
Join