Menu

Simple Authentication
Simple Authentication methods for faster development

Overview

Working with tokens

Leaf provides you with the $jwt object which includes various methods for creating and parsing token data....but we do not advice directly using the $jwt object. For this reason, a special $authentication object has been created to work with all the $jwt data. To use $authentication,

$authentication = new Leaf\Core\Authentication();

Authentication methods:

generateSimpleToken()

This method generates a new JSON Web Token. It takes 2 arguments. check out JWT for more info on JWT params

  • (int) - A user id to encode
  • (string) - A secret phrase to encode with the token

$token = $authentication->generateSimpleToken(24', 'LEAF PHP SECRET CODE 1442');

generateToken()

This method generates a new JSON Web Token: the same as generateSimpleToken(), the only difference is that this method gives you more control over the token i.e. expiry time..... It takes 4 parameters. check out JWT for more info on JWT params

  • (int) - A user id to encode
  • (int) - Expiry time....this is the amount of time the token will last from the current time
  • (string) - A secret phrase to encode with the token
  • (string) - iss - issuer....identifies the principal that issued the JWT - default is localhost

$token = $authentication->generateToken(24', 3600, 'LEAF PHP SECRET CODE 1442', 'localhost');

validateToken()

This method is used to confirm the identity of a token from an authorization header

$token = $authentication->validateToken();

getBearerToken()

This method is used to get the bearer token from an authorization header

$token = $authentication->getBearerToken();

getAuthorizationHeader()

This method is used to an authorization header

$authHeader = $authentication->getAuthorizationHeader();

Next Steps

Re-routing to index.php
Simple Routing
Request
Response