1. Go to this page and download the library: Download lahaxearnaud/laravel-token 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/ */
$tokenStr = Token::getTokenValueFromRequest();
/**
* if the token is crypt do :
* $tokenStr = Token::uncryptToken($tokenStr);
**/
$tokenValid = true;
try {
// find the token
$token = $token->findByToken($tokenStr, $userId);
// test the token validity
if (Token::isValidToken($token)) {
// do what you need to do
// delete the token
Token::burn($token);
} else {
$tokenValid = false;
}
} catch (TokenNotFoundException $e) {
$tokenValid = false;
}
if($tokenValid) {
// manage errors
}
Route::get('/token-protected', array('before' => 'token', function () {
echo "I am token protected";
}));
Route::get('/login-by-token', array('before' => 'token.auth', function () {
echo Auth::user()->username;
}));
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Lahaxearnaud\LaravelToken\Models\UserTokenInterface;
class User extends Eloquent implements UserInterface, RemindableInterface, UserTokenInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function loggableByToken()
{
return true;
}
}