我想用 codeigniter 中的登录时间更新 jwt 令牌
Posted
技术标签:
【中文标题】我想用 codeigniter 中的登录时间更新 jwt 令牌【英文标题】:I want to update jwt token with login time in codeigniter 【发布时间】:2021-06-28 11:25:36 【问题描述】:我想在登录时更新 jwt 令牌并将该 jwt 令牌设置到我的数据库用户表中。 我希望你一定会帮助我。 谢谢
这是我的用户控制器
public function canLogin()
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'GET')
json_output(404, array('message' => 'Method must be a POST'));
else
$params = (array) json_decode(file_get_contents('php://input'), true);
$email = $params['email'];
$password = $params['password'];
// $tokenData = $this->authorization_token->generateToken($email, $password);
// $final = array();
// $final['token'] = $tokenData;
// json_output(200, array('code' => 200, 'data' => $final));
if ($email && $password == true)
$data = $this->Users_Model->canLogin($email);
if ($data != null)
$check_password = password_verify($password, $data[0]['password']);
if ($check_password)
json_output(200, array('code' => 200, 'message' => "Successfully Login", "data" => $data));
else
json_output(200, array('code' => 400, 'message' => "Invalid Password"));
else
json_output(200, array('code' => 400, 'message' => "Email not Found"));
else
json_output(200, array('code' => 400, 'message' => "Email or Password Shouldn't be null"));
这是我的用户模型函数:
public function canLogin($email)
$this->db->select('*');
$this->db->where('email', $email);
// $query = $this->db->get($this->users);
$query = $this->db->get('users');
// $row = $query->row();
return $query->result_array();
【问题讨论】:
只是一个提示,但我会使用405
而不是404
,因为它实际上告诉客户端“方法不允许”。
【参考方案1】:
我自己解决了我的问题,并在有人登录时从 API 获得响应,它会生成一个唯一令牌并将其更新到数据库中。
public function canLogin()
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'GET')
json_output(404, array('message' => 'Method must be a POST'));
else
$params = (array) json_decode(file_get_contents('php://input'), true);
$email = $params['email'];
$password = $params['password'];
if ($email && $password == true)
$data = $this->Users_Model->canLogin($email);
if ($data != null)
$check_password = password_verify($password, $data[0]['password']);
if ($check_password)
$email_data = array('email' => $email, 'password' => $password);
$tokenData = $this->authorization_token->generateToken($email_data);
$jwt_token = $this->Users_Model->tokenUpdate($email, $tokenData);
if ($jwt_token)
json_output(200, array('code' => 200, 'data' => $data, 'token' => $tokenData));
else
json_output(200, array('code' => 200, 'message' => 'Something Went Wrong'));
else
json_output(200, array('code' => 400, 'message' => "Invalid Password"));
else
json_output(200, array('code' => 400, 'message' => "Email not Found"));
else
json_output(200, array('code' => 400, 'message' => "Email or Password Shouldn't be null"));
我的模特:
public function tokenUpdate($email, $token)
$this->db->where('email', $email);
$this->db->update('users',array('token' => $token));
return true;
最后,获取令牌作为响应...
【讨论】:
以上是关于我想用 codeigniter 中的登录时间更新 jwt 令牌的主要内容,如果未能解决你的问题,请参考以下文章
没有登录会话,任何人都无法访问 codeigniter 4 中的控制器
使用 cpanel 的 codeigniter 密码保护目录