Example of thinkphp5 Framework API token Authentication Function

  • 2021-12-11 07:05:06
  • OfStack

This article illustrates the thinkphp5 framework API token authentication functionality. Share it for your reference, as follows:

Instructions for use: Generate token and refresh_token for refresh when logging in, Back to the client, The client receives and saves the local localStorage, Each provider comes with token, The back-end verifies that token exists and the back-end can perform the next action. If not, return token expired, the client calls the refresh interface to pass in token and refresh_token, the server carries on the verification, the verification by regenerating the new token to save the database, return to the client to refresh the local token access can continue, when the refresh_token verification fails, clear the database token, expiration time and other information

Simple token generation function (common function file common)


function create_token($id,$out_time){
  return substr(md5($id.$out_time),5,26);
}

Verify the login method (model)


public function checkLogin($username,$passwd){
    $driver = self::field('driver_id,passwd')->where('zhanghao',$username)->whereOr('phone',$username)->find();
    if (empty($driver)){
      $this->error = ' The account number does not exist ';
      return false;
    }
    if ($driver['passwd'] != md5($passwd)){
      $this->error = " Incorrect password ";
      return false;
    }
    //$out_time = strtotime('+ 1 days');
    $out_time = strtotime('+ 1 minutes');
    $token = create_token($driver['driver_id'],$out_time);
    if(false===self::save(['token'=>$token,'time_out'=>$out_time],['driver_id'=>$driver['driver_id']])){
      $this->error = ' Log in failed ';
      return false;
    }
    $refresh_token_out_time = strtotime('+ 5 days');
    $refresh_token = create_token($driver['driver_id'],$refresh_token_out_time);
    Cache::set("token",$token,60);
    Cache::set("driver_id",$driver['driver_id'],$refresh_token_out_time);// Settings ID Expiration time and update of token Adj. token Time 1 Sample is used to obtain user information when updating 
    Cache::set('refresh_token',$refresh_token,$refresh_token_out_time);
    return ['token'=>$token,'refresh_token'=>$refresh_token,'in_expire'=>$out_time];
}

token Refresh Method (Model)


public function refreshToken($refresh_token,$token){
    if (!isset(Cache::get('refresh_token')) or Cache::get('refresh_token')!=$refresh_token){
      $this->error = ' Refresh token Failure ';
      return false;
    }
    $cache_driver_id = Cache::get('driver_id');
    $driver = self::field('driver_id,passwd')->where('driver_id',$cache_driver_id)->where('token',$token)->find();
    if (empty($driver)){
      $this->error = ' Parameter error ';
      return false;
    }
    $out_time = strtotime('+ 1 days');// New expiration time 
    $token = create_token($driver['driver_id'],$out_time);// Update token
    if(false===self::save(['token'=>$token,'time_out'=>$out_time],['driver_id'=>$driver['driver_id']])){
      Cache::clear($token);
      $this->error = ' Refresh failed ';
      return false;
    }
    Cache::set("token",$token,864000);
    return ['token'=>$token,'in_expire'=>$out_time];
}

Exit method (model)


public function logout($token,$refresh_token=''){
    $driver = self::field('driver_id,passwd')->where('token',$token)->find();
    self::save(['token'=>'','time_out'=>''],['token'=>$token]);
    Cache::clear('token');
    Cache::clear('refresh_token');
}

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: