PHP code example of dyorg / slim-token-authentication
1. Go to this page and download the library: Download dyorg/slim-token-authentication library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dyorg / slim-token-authentication example snippets
$authenticator = function($request, TokenAuthentication $tokenAuth){
# Search for token on header, parameter, cookie or attribute
$token = $tokenAuth->findToken($request);
# Your method to make token validation
$user = User::auth_token($token);
# If occured ok authentication continue to route
# before end you can storage the user informations or whatever
...
};
$app = new App();
$app->add(new TokenAuthentication([
'path' => '/api',
'authenticator' => $authenticator
]));
...
$app = new App();
$app->add(new TokenAuthentication([
'path' => '/api', /* or ['/api', '/docs'] */
'authenticator' => $authenticator
]));
...
$app->add(new TokenAuthentication([
'path' => '/api',
'authenticator' => $authenticator,
'header' => 'Token-Authorization-X',
'regex' => '/Basic\s+(.*)$/i', /* for without token type can use /\s+(.*)$/i */
]));